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

来源:互联网 发布:网站群发软件 编辑:程序博客网 时间:2024/05/29 03:26


BOOL GetTimeInf(TCHAR *ptszTimeFormatCtrl, TCHAR *ptszTimeBuf, DWORD dwBufLen){// 0. variable definitionSYSTEMTIME Reg_systime;                             // System time from regist_locale_t locale = NULL;                             // local to determine time show stylestruct tm Reg_showtime;                              // Reg show time from System time// 1. Init Reg_systimeZeroMemory(&Reg_systime, 0, sizeof(SYSTEMTIME));DWORD dwTimeLen = sizeof(SYSTEMTIME);// 2. Read system time from regGetSystemTime(&Reg_systime);// 3. Get Reg_showtime from Reg_systime// month and year need to modify, others are the sameReg_showtime.tm_sec = Reg_systime.wSecond;         // SecondReg_showtime.tm_min = Reg_systime.wMinute;         // MinuteReg_showtime.tm_hour = Reg_systime.wHour;          // HourReg_showtime.tm_mday = Reg_systime.wDay;           // DayReg_showtime.tm_mon = Reg_systime.wMonth -1;       // MonthReg_showtime.tm_year = Reg_systime.wYear - 1900;   // YearReg_showtime.tm_wday = Reg_systime.wDayOfWeek;     // Day of week// 4. Get the implementation-defined native environmentlocale = _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);}


0 0
原创粉丝点击