friedo friedo -4 years ago 124
C Question

Get the time zone GMT offset in C

I'm using the standard

function to turn a
struct tm
into an epoch time value. The
tm
fields are populated locally, and I need to get the epoch time as GMT.
tm
has a
gmtoff
field to allow you to set the local GMT offset in seconds for just this purpose.

But I can't figure out how to get that information. Surely there must be a standard function somewhere that will return the offset? How does
localtime
do it?

Answer Source

I guess I should have done a bit more searching before asking. It turns out there's a little known timegm function which does the opposite of gmtime. It's supported on GNU and BSD which is good enough for my purposes. A more portable solution is to temporarily set the value of the TZ environment variable to "UTC" and then use mktime, then set TZ back.

But timegm works for me.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download