c++ - why gmtime and localtime give me the same result? -
i have following test code see difference between gmtime , localtime. give me same result: 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;
edit:
i in eastern time zone.
edit2:
updated code use diff struct, got same result:
time_t now; time(&now); time_t now2; time(&now2); tm *ptimestruct = gmtime(&now); tm *plocaltimestruct = localtime(&now2);
you need copy values between calls gmtime
, localtime
:
the return value points statically allocated struct might overwritten subsequent calls of date , time functions.
says man page on system. that's common behaviour @ least on linux.
Comments
Post a Comment