By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,952 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,952 IT Pros & Developers. It's quick & easy.

Time offset from UTC

P: n/a
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
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,

Nov 15 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.