S
sags5495
Hello all,
For a program I want to fix, I need to find a portable way to get
the offset from local time to UTC (coming from the Time Zone plus
any daylight savings in effect).
At first glance, the following seems to be OK:
time_t t;
int timeoffset;
t = time (NULL);
timeoffset = (int) difftime (t, mktime(gmtime(&t))); /* seconds */
My concern is the above code's portability. I'm thinking at:
(a) Is mktime() always the inverse of localtime(), or it may use
implementation-dependent fields in struct tm, that may specify
TZ info, for it's calculations? (In such a case, gmtime() may store
UTC+00:00 in the structure, and I get timeoffset always 0.)
(b) Assuming localtime() succeeds, is there any good reason for
gmtime()
to fail? (like a system and C run-time that has absolutely no
knowledge about time-zones, not even an arbitray default.)
Any comments, suggestions? Is there a better way?
Thanks in advance,
sags
For a program I want to fix, I need to find a portable way to get
the offset from local time to UTC (coming from the Time Zone plus
any daylight savings in effect).
At first glance, the following seems to be OK:
time_t t;
int timeoffset;
t = time (NULL);
timeoffset = (int) difftime (t, mktime(gmtime(&t))); /* seconds */
My concern is the above code's portability. I'm thinking at:
(a) Is mktime() always the inverse of localtime(), or it may use
implementation-dependent fields in struct tm, that may specify
TZ info, for it's calculations? (In such a case, gmtime() may store
UTC+00:00 in the structure, and I get timeoffset always 0.)
(b) Assuming localtime() succeeds, is there any good reason for
gmtime()
to fail? (like a system and C run-time that has absolutely no
knowledge about time-zones, not even an arbitray default.)
Any comments, suggestions? Is there a better way?
Thanks in advance,
sags