I’ve the following piece of code to find the difference between the UTC and local time zone.
struct tm *local_time, *gmt_time;
time_t t = time(NULL);
local_time = localtime(&t);
gmt_time = gmtime(&t);
int y = mktime(local_time);
int x = mktime(gmt_time);
tzone_diff = y - x;
This doesn’t work. However if i move the statement gmt_time = gmtime(&t) below int y = mktime(local_time); it works. That is the following piece of code works:
struct tm *local_time, *gmt_time;
time_t t = time(NULL);
local_time = localtime(&t);
int y = mktime(local_time);
gmt_time = gmtime(&t);
int x = mktime(gmt_time);
tzone_diff = y - x;
This seems wierd to me.. Any clues?
localtime and gmtime both returns a pointer to a struct tm. This struct tm is defined somewhere in the C library, and can easily be the same for both functions. Something like the below.