473,563 Members | 2,683 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to translate from UTC/GMT time to local time?

Suppose one has a database of UTC times that are from different dates in the past.
The problem with translating those times to a local time is that one does not know
for each UTC time whether the local time at that same moment had daylight savings
time in effect.

If one has a local time it is easier to translate it into UTC time. One can get the
UTC time and one can even calculate the number of hours difference between them as
follows:

System.DateTime CurrTime = System.DateTime .Now;
System.DateTime CurrUTCTime = CurrTime.ToUniv ersalTime();
long DiffFactorTicks = CurrUTCTime.Tic ks - CurrTime.Ticks;
// NumTicksDiff
// 100 nanosecond ticks: 10,000,000 ticks per second?
double DiffFactorSecon ds = DiffFactorTicks/10000000;
double DiffFactorHours = DiffFactorSecon ds/3600; // this'll be positive and
will be subtracted from date/times.

If one subtracts DiffFactorHours from a UTC time one will get one's local time. But
that ONLY works if the UTC/local difference is the same as the current difference.

I do not see how to start with a UTC time and get to a local time because I do not
see how starting with a UTC time to find out what the hours difference is to local.
The problem is that daylight savings time might or might not be in effect.

Also, how to go to a local time that is other than the local time of the server that
you happen to be running?
Dec 16 '05 #1
3 23738
Try using the System.DateTime 's ToLocalTime() method. It will figure it
out for you. You don't need to do any of that DiffFactorTicks stuff.

http://msdn.microsoft.com/library/de...ltimetopic.asp

DateTime updateDate;

// assuming dr is a IDbDataReader that returns a date
// in the first column

updateDate = dr.GetDateTime( 0);

// You know that dates are stored in your DB as UTC,
// so you know that updateDate now holds a UTC. So call ToLocalTime()

DateTime localDate = updateDate.ToLo calTime();
Joshua Flanagan
http://flimflan.com/blog
Dec 17 '05 #2
Is there any way to tell the class to use a different time zone as the local time zone?

My problem is that my code will be on a server and I may know that a user is in some
time zone. It is not the server's time zone. So how to reliably translate from UTC to
that user's time zone given that at different times of the year the user might or
might not be in daylight saving's time.

Joshua Flanagan wrote:
Try using the System.DateTime 's ToLocalTime() method. It will figure it
out for you. You don't need to do any of that DiffFactorTicks stuff.

http://msdn.microsoft.com/library/de...ltimetopic.asp
DateTime updateDate;

// assuming dr is a IDbDataReader that returns a date
// in the first column

updateDate = dr.GetDateTime( 0);

// You know that dates are stored in your DB as UTC,
// so you know that updateDate now holds a UTC. So call ToLocalTime()

DateTime localDate = updateDate.ToLo calTime();
Joshua Flanagan
http://flimflan.com/blog

Dec 18 '05 #3
Ok, I don't know of any way to tell the DateTime class to use a
different time zone from the current computer.

If you knew the start/end times for the daylight savings time in the
user's timezone, you might be able to do the computations yourself.

Look into System.TimeZone and System.Globaliz ation.DaylightT ime. You may
need to derive from one of them or do some reflection to get at
non-public methods/constructors. Use Lutz Roeder's Reflector to explore
the innards of those classes and see if you can find a way to override
the timezone.

Of course, using the non-public interface of a class has its dangers
(for one, it is not guaranteed to work in the future), so proceed with
caution.
Dec 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
12879
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone here put me straight on the following: I had a look at the time() function came across this: "To clarify, it seems this function returns the...
0
1296
by: Bill Borg | last post by:
Hello all, I need to know the local time for users of a global system. Here's the scenario: My server (PDT time) hosts commerce for companies around the world. It's fair to assume that those companies' customers are all local to that company. With each transaction I get the current time, and store as UTC/GMT. But when I want to show the...
5
5778
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,
4
4778
by: Anony Mous | last post by:
Hi, Is there a function that would allow me to set the local time while the server is running? I can't seem to find anything on this... Thanks, Peter ---------------------------(end of broadcast)---------------------------
1
8410
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.
1
5624
by: Generic Usenet Account | last post by:
I am trying to find the best way to get the difference between UTC and the local time? Here's a small code snippet that I have written for this purpose. Is there a better way to do this? Thanks, Bala /*************************************************************************/ time_t getTimeOffset() {
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:
2
18159
by: ZR | last post by:
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...
1
3075
by: Josef Dalcolmo | last post by:
Hello, I have tried this only on Windows XP. in Python 2.4 os.path.getmtime() used to return an integer representing the local time. in Python 2.5 os.path.getmtime() reports a float representing the GMT of the file's modification time.
0
7659
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
7882
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
8103
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...
0
7945
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
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3634
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
3618
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2079
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.