SYSTEMTIME 转化为 tm struct 并根据本地的设置输出

本文介绍了如何在C++中将SYSTEMTIME结构转换为tm结构,并根据系统的本地设置进行时间显示。通过示例代码,展示了转换过程和使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


BOOL GetTimeInf(TCHAR *ptszTimeFormatCtrl, TCHAR *ptszTimeBuf, DWORD dwBufLen)
{
// 0. variable definition
SYSTEMTIME Reg_systime;                             // System time from regist
_locale_t locale = NULL;                             // local to determine time show style
struct tm Reg_showtime;                              // Reg show time from System time

// 1. Init Reg_systime
ZeroMemory(&Reg_systime, 0, sizeof(SYSTEMTIME));
DWORD dwTimeLen = sizeof(SYSTEMTIME);


// 2. Read system time from reg
GetSystemTime(&Reg_systime);
// 3. Get Reg_showtime from Reg_systime
// month and year need to modify, others are the same
Reg_showtime.tm_sec = Reg_systime.wSecond;         // Second
Reg_showtime.tm_min = Reg_systime.wMinute;         // Minute
Reg_showtime.tm_hour = Reg_systime.wHour;          // Hour
Reg_showtime.tm_mday = Reg_systime.wDay;           // Day
Reg_showtime.tm_mon = Reg_systime.wMonth -1;       // Month
Reg_showtime.tm_year = Reg_systime.wYear - 1900;   // Year
Reg_showtime.tm_wday = Reg_systime.wDayOfWeek;     // Day of week
// 4. Get the implementation-defined native environment
locale = _create_locale(LC_ALL,"");
if (locale == NULL)
{
   OutputDebugString(TEXT("_create_locale() failed\r\n"));
   return FALSE;
}
// 5. Set ptszTimeBuf value
_tcsftime_l(ptszTimeBuf, dwBufLen, ptszTimeFormatCtrl, &Reg_showtime, locale);
if (locale)
{
   _free_locale(locale);
}
return TURE;
}


用法:

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR *ptszTimeFormat = TEXT("%#c"); //TEXT("%c")
TCHAR tszTimeBuf[100] = {0};
BOOL bres = GetTimeFromInf(ptszTimeFormat, tszTimeBuf, 100);
if (!bres)
{
OutputDebugString(TEXT("GetTimeFromRegist failed !\r\n"));
return 0;
}
TCHAR tszDebugMsg[100] ={0};
_stprintf_s(tszDebugMsg, 100, TEXT("%s Format UTC Time is %s\r\n"), ptszTimeFormat, tszTimeBuf);
MessageBox(NULL, tszDebugMsg, TEXT("test"), MB_OK);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值