Time offset from UTC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top