views:

108

answers:

2

My code receives a time_t from an external source. However, that time_t isn't acutally based on UTC Epoch time, along with it I get a timezone string (eg, EDT, PST, etc), and its based on this offset'ed epoch. I need to convert this to a true UTC Epoch based time_t.

Additionally, I need to be able to go in the opposite direction, taking a UTC Epoch based time_t and a timezone, and create the offsetted time_t, but in this situation instead of having a timezone like EDT/PST), etc, I have a Unix style timezone description like "America/New York" and need to convert to the correct timezone given daylight savings, so I'd need to get back from the algorithm, both an offsetted time_t, and the correct descriptor (EDT,EST).

I'm pretty sure I can pull this off by temporarily changing tzset() and some combination of conversions between broken-down time, time_t and timeval, but doing this always makes my brain hurt and makes me feel like I'm missing something obvious. Can anyone recommend some code or sudo-code to do this or at least a proper approach?

A: 

would any of these functions help you? localtime(), gmtime(), etc.

paquetp
These are what I usually use for tasks like this along with several other time manipulation commands. What I'm having trouble with is figuring out the correct chaining of all the different commands and data formats and conversions to achieve what I want, since none of them gets the actual job done.
bdk
+1  A: 

time_t is in seconds, so just offset your time_t values by 3600 times the number of hours the timezone is offset by. As long as you have the offset specifically identified (i.e. EST or EDT instead of US/Eastern or EST5EDT or whatnot) then this is really simple and not prone to errors.

R..
Thanks, in one direction I do have the EDT/EST, though in the other I just have America/New York. Is there a standard function to take a timezeone (EDT,EST,etc) and return the hour offset?
bdk
No, and unfortunately the timezone names are not globally unique. If you only need to support US timezones, you can just hard-code them yourself and be done with it.
R..