473,543 Members | 2,466 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?

"subtractin g ### 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 18142
In article <12************ *****@newsread3 .news.pas.earth link.net>,
ZR <ZR@ABC.comwrot e:
>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.
>"subtractin g ### 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
>>"subtractin g ### 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
5776
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 what it would currently be in Pacific time. Taking in account Daylight Savings Time Thanks, Ed,
2
9554
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 '(01C5288B to decimal) a.dwLowDateTime = -539522172 '(??????? by 84 8B D7 DF) MsgBox (GetFileToSystemDate(a, False))
2
4942
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 19 Oct 2005 13:40:23 -0400
29
2608
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 server back-end. I'm doing my best to learn about SQL server, and I plan to leave the front-end more or less as-is, just linking to the SQL server...
1
8405
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 time to the query. I thanku all in advance.
6
21177
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
14582
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 = '2007-03-13T15:00:00Z' I want to convert it to my local time. start_time = time.mktime(time.strptime(start_time, '%Y-%m-%dT%H:%M:
1
3575
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 some files and work on it. Let say, I want add new page to web project named websiteOrder.sln, i will open websiteOrder.sln in my local computer,...
8
6036
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 app. 3. I need to convert the received date string in to a date w.r.t my local time zone.
0
7408
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7590
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7735
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7688
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
4895
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3391
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.