我有以下測試代碼來查看gmtime和localtime之間的區別。但他們給我相同的結果:UTC:2013-05-02T13:59:58地方:2013-05-02T13:59:58爲什麼gmtime和localtime給我同樣的結果?
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;
編輯:
我在東部時間區。
EDIT2:
更新的代碼使用DIFF結構,但得到了同樣的結果:
time_t now;
time(&now);
time_t now2;
time(&now2);
tm *pTimeStruct = gmtime(&now);
tm *plocalTimeStruct = localtime(&now2);
你在什麼系統上? – 2013-05-02 18:06:22
linux機器,fedora – 5YrsLaterDBA 2013-05-02 18:14:49