473,325 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 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 18047
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ECVerify.com | last post by:
I am trying to convert a computers local time to Pacific time, I can get it back to UTC pretty easy but I actually want to convert whatever the local time is on the computer I want to calculate...
2
by: yxq | last post by:
There are 8 bytes binary value stored date and time in Registry. 84 8B D7 DF 8B 28 C5 01 I want to convert the binary value to date using VB.NET. Dim a As FILETIME a.dwHighDateTime = 29698187...
2
by: yxq | last post by:
Hello, I found there are some date formats in the email header, for example: Fri, 23 Sep 2005 08:51:56 +0800 Sat, 17 Sep 2005 09:08:07 Wed Oct 19 13:40:23 2005 19 Oct 2005 13:40:23 +0000...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
1
by: XML newbie: Urgent pls help! | last post by:
How to Convert local time(eg EST) on local machine to GMT. I am using VB.Net 2005. Then I need to subtract 1 minute(or 1-100) minute selected by the user in the combobox and pass it as start...
6
by: vunet.us | last post by:
How can I convert UTC time such as 1173451235415 to a local time I can read? Thank you
1
by: davelist | last post by:
I'm guessing there is an easy way to do this but I keep going around in circles in the documentation. I have a time stamp that looks like this (corresponding to UTC time): start_time =...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
8
by: deepak_kamath_n | last post by:
Hello, I have the following scenario: 1. My application receives the date from another application as a string 2. The other application is running in a different time zone as compared to my...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.