why gmtime and localtime give me the same result?

c, c++

Solution

You need to copy the values between the calls to `gmtime` and `localtime`:

The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.

says the man page on my system. That's common behaviour at least on Linux.

Problem

I have following test code to see the difference between gmtime and localtime. But they give me the 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 am in Eastern time zone. EDIT2: updated code use diff struct, but got the same result: ``` time_t now; time(&now); time_t now2; time(&now2); tm *pTimeStruct = gmtime(&now); tm *plocalTimeStruct = localtime(&now2); ```

Original source