Convert GMT to Local Time

Z

ZR

Hello,
I need to convert a GMT time string to local time. I can fill out a "tm"
structure with the GMT time string. Are there any standard C (or OS) time
functions that will allow me to do this?

"subtracting ### hours depending on the locale" is not an option because I
need this to work on any machine anywhere in the world, unless there is a
way to get the ### (from OS or machine using a standard C or C++ system
function calls).

I searched this on the web and didn't figure this out.

Thanks for any help!
 
D

Dave Vandervies

Hello,
I need to convert a GMT time string to local time. I can fill out a "tm"
structure with the GMT time string. Are there any standard C (or OS) time
functions that will allow me to do this?

You have gmtime and localtime for going the other direction, but I only
see mktime (which operates on local time) to go the direction you want.

If there is something I'm missing, I'd also like to know what, so I'm
going to go ahead and claim that what I see is all you get and let
somebody who knows of one correct me if that's wrong.


"subtracting ### hours depending on the locale" is not an option because I
need this to work on any machine anywhere in the world, unless there is a
way to get the ### (from OS or machine using a standard C or C++ system
function calls).

It seems to me you could do it by:
1. Use mktime to get a time_t representing the time that that struct tm
would represent if it were local time
2. Use gmtime to get a struct tm from that time_t representing that time
in GMT
3. Calculate the time offset using your two different 'struct tm's
(Don't forget to handle offsets that aren't whole hours and wraparound
at the end of a day)
4. Apply this offset to get the broken-down time in local time
5. Use mktime to get a time_t from that struct tm

But that's kind of ugly just to do the inverse of gmtime.

If you can get away with assuming that the offset between the time
represented by a time_t and a fixed reference time is a linear function
of the value of the time_t, there's a slightly less ugly way to do it:

/*We have a GMT time in my_tm and we want the corresponding time_t*/
/*bad_time is what the time would be if my_tm represented local time*/
time_t bad_time = mktime(&my_tm);
/*time_diff is the offset between local time and GMT, in time_t units
We're depending on this difference representing the same wall-clock time
difference everywhere in time_t's range (likely but not guaranteed by
the C standard).
*/
time_t diff = mktime(gmtime(&bad_time)) - bad_time;
/*Now that we know the offset, apply it to get the correct time*/
time_t good_time = bad_time + diff;



If you're going to do either of these, you need to decide whether possible
errors near a DST switch matter, and think carefully about how to avoid
them if they do.


dave
 
Z

ZR

"subtracting ### hours depending on the locale" is not an option because I
It seems to me you could do it by:
1. Use mktime to get a time_t representing the time that that struct tm
would represent if it were local time
2. Use gmtime to get a struct tm from that time_t representing that time
in GMT
3. Calculate the time offset using your two different 'struct tm's
(Don't forget to handle offsets that aren't whole hours and wraparound
at the end of a day)
4. Apply this offset to get the broken-down time in local time
5. Use mktime to get a time_t from that struct tm

Thanks for the suggestion. That's what I thought off after I post the
message. I'll make sure the offset is calculated properly.
But that's kind of ugly just to do the inverse of gmtime.

If you can get away with assuming that the offset between the time
represented by a time_t and a fixed reference time is a linear function
of the value of the time_t, there's a slightly less ugly way to do it:

/*We have a GMT time in my_tm and we want the corresponding time_t*/
/*bad_time is what the time would be if my_tm represented local time*/
time_t bad_time = mktime(&my_tm);
/*time_diff is the offset between local time and GMT, in time_t units
We're depending on this difference representing the same wall-clock time
difference everywhere in time_t's range (likely but not guaranteed by
the C standard).
*/
time_t diff = mktime(gmtime(&bad_time)) - bad_time;
/*Now that we know the offset, apply it to get the correct time*/
time_t good_time = bad_time + diff;

This is even better.

Thanks!
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top