Windows API一日一练(70)GetSystemTime和GetLocalTime函数

<iframe align="center" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog336280.html" frameborder="0" width="336" scrolling="no" height="280"></iframe>
时间是一个非常重要的信息,比如写 LOG 时,就需要把时间输出来,跟踪程序是什么时候出错的。或者当你开发一个银行交易系统时,就要记录当前交易的时间,以便后面可以输出报表,打印给信用卡用户。根据不同的需求,可能需要使用不同的时间,目前有 UTC 和本地时间。 UTC 是格林威治时间,也就是全球标准时间。本地时间就是相对于 UTC 而言的,比如中国北京是在东 8 区,相对于 UTC 就多了 8 个小时。一般使用到的时间都是使用本地时间,也就是调用函数 GetLocalTime
函数 GetSystemTime GetLocalTime 声明如下:
WINBASEAPI
VOID
WINAPI
GetSystemTime(
__out LPSYSTEMTIME lpSystemTime
);
WINBASEAPI
VOID
WINAPI
GetLocalTime(
__out LPSYSTEMTIME lpSystemTime
);
lpSystemTime 是获取系统时间的结构。
调用函数的例子如下:
#001
#002// 获取系统时间。
#003// 蔡军生 2007/11/11 QQ:9073204 深圳
#004void TestSystem(void)
#005{
#006 // 获取系统的 UTC 时间。
#007 SYSTEMTIME stUTC;
#008 ::GetSystemTime(&stUTC);
#009
#010 // 显示时间的间隔。
#011 const int nBufSize = 256;
#012 TCHAR chBuf[nBufSize];
#013 wsprintf(chBuf,_T("UTC: %u/%u/%u%u:%u:%u:%u %d\r\n"),
#014 stUTC.wYear, stUTC.wMonth, stUTC.wDay,
#015 stUTC.wHour, stUTC.wMinute, stUTC.wSecond,
#016 stUTC.wMilliseconds,stUTC.wDayOfWeek);
#017 OutputDebugString(chBuf);
#018
#019
#020 // 获取当地的时间。
#021 SYSTEMTIME stLocal;
#022 ::GetLocalTime(&stLocal);
#023
#024 // 显示时间的间隔。
#025 wsprintf(chBuf,_T("Local: %u/%u/%u%u:%u:%u:%u %d\r\n"),
#026 stLocal.wYear, stLocal.wMonth, stLocal.wDay,
#027 stLocal.wHour, stLocal.wMinute, stLocal.wSecond,
#028 stLocal.wMilliseconds,stLocal.wDayOfWeek);
#029 OutputDebugString(chBuf);
#030
#031}
#032
上面两个函数在我测试时输出的结果,如下:
UTC: 2007/11/111:53:1:46 0
Local: 2007/11/119:53:1:46 0



你可能感兴趣的:(html,windows,qq)