我有以下测试代码来查看 gmtime 和 localtime 之间的区别。但他们给了我同样的结果:UTC:2013-05-02T13:59:58 Local:2013-05-02T13:59:58
time_t now;
time(&now);
tm *pTimeStruct = gmtime(&now);
tm *plocalTimeStruct = localtime(&now);
string timeStr = "";
char timeBuf[64] = {'\0'};
sprintf(timeBuf,"UTC:%-4.4d-%-2.2d-%-2.2dT%-2.2d:%-2.2d:%-2.2d "
"Local:%-4.4d-%-2.2d-%-2.2dT%-2.2d:%-2.2d:%-2.2d",
(pTimeStruct->tm_year + 1900),
(pTimeStruct->tm_mon + 1),
pTimeStruct->tm_mday,
pTimeStruct->tm_hour,
pTimeStruct->tm_min,
pTimeStruct->tm_sec,
(plocalTimeStruct->tm_year + 1900),
(plocalTimeStruct->tm_mon + 1),
plocalTimeStruct->tm_mday,
plocalTimeStruct->tm_hour,
plocalTimeStruct->tm_min,
plocalTimeStruct->tm_sec);
timeStr += timeBuf;
cout << timeStr << endl;
编辑:
我在东部时区。
编辑2:
更新的代码使用 diff 结构,但得到了相同的结果:
time_t now;
time(&now);
time_t now2;
time(&now2);
tm *pTimeStruct = gmtime(&now);
tm *plocalTimeStruct = localtime(&now2);