1.GetLocalTime与GetSystemTime区别
GetLocalTime和GetSystemTime是Windows API 函数,用来获取当地的当前系统和格林威治日期和时间,北京时间和格林威治时间两者相差了8小时。(使用前需要include<Windows.h>)
函数原型 :
VOID GetLocalTime(
LPSYSTEMTIME lpSystemTime //address of system times structure
);
参数说明:
lpSystemTime: 指向一个用户自定义包含日期和时间信息的类型为 SYSTEMTIME 的变量,
该变量用来保存函数获取的时间信息。此函数会把获取的系统时间信息存储到SYSTEMTIME结构体里边
typedef struct _SYSTEMTIME {
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME, *LPSYSTEMTIME;
2.时间戳生成
对Windows系统:
std::string getTimeStamp()
{
char buf[1024];
SYSTEMTIME systime;
GetLocalTime(&systime);
sprintf_s(buf, "%04d/%02d/%02d-%02d:%02d:%02d.%03d ", systime.wYear, systime.wMonth, systime.wDay,
systime.wHour, systime.wMinute, systime.wSecond, systime.wMilliseconds);
return std::string(buf);
}
在写函数时即可调用getTimeStamp函数,精确到毫秒级输出函数执行时的时刻,如下。