Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why gmtime and localtime give me the same result?

Tags:

c++

c

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);
like image 464
5YrsLaterDBA Avatar asked May 02 '13 18:05

5YrsLaterDBA


People also ask

What is the use of Gmtime?

Description. The gmtime() function breaks down the time value, in seconds, and stores it in a tm structure, defined in <time. h>. The value time is usually obtained by a call to the time() function.

Can Gmtime return null?

The gmtime() function converts the calendar time timep to broken-down time representation, expressed in Coordinated Universal Time (UTC). It may return NULL when the year does not fit into an integer.

Is Gmtime thread safe?

APPLICATION USAGE. The gmtime_r() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call.


3 Answers

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.

like image 74
Daniel Fischer Avatar answered Sep 29 '22 09:09

Daniel Fischer


You can also use gmtime_r and localtime_r, which are thread safe and store the data in a user-supplied struct.

struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime_r(const time_t *timep, struct tm *result);

Note for Windows users: _gmtime_s and _localtime_s are the Microsoft versions.

errno_t _gmtime_s(struct tm* _tm, const __time_t* time);
errno_t _localtime_s(struct tm* _tm, const time_t *time);
like image 34
jramos Avatar answered Sep 29 '22 10:09

jramos


I also had this problem and solved it by using memcpy:

time_t t = time(NULL);  

tm* gmt = (tm*)malloc(sizeof(tm));
memcpy(gmt, gmtime(&t), sizeof(tm));

tm* loc = localtime(&t);

cout << asctime(gmt) << endl;
cout << asctime(loc) << endl;   

free(gmt);
like image 25
Eldar Agalarov Avatar answered Sep 29 '22 11:09

Eldar Agalarov