472,119 Members | 1,888 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,119 software developers and data experts.

Convert GMT to Local Time

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!
Mar 16 '07 #1
2 17595
In article <12*****************@newsread3.news.pas.earthlink. net>,
ZR <ZR@ABC.comwrote:
>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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
The "used car salesman" has an unjust reputation. It's car salespeople
in general, whether the cars are new or used.
--Alan J Rosenthal in the scary devil monastery
Mar 17 '07 #2
ZR
>>"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
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!
Mar 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by ECVerify.com | last post: by
1 post views Thread by XML newbie: Urgent pls help! | last post: by
6 posts views Thread by vunet.us | last post: by
1 post views Thread by davelist | last post: by
8 posts views Thread by deepak_kamath_n | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.