473,379 Members | 1,216 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,379 software developers and data experts.

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.ToUniversalTime();
long DiffFactorTicks = CurrUTCTime.Ticks - CurrTime.Ticks;
// NumTicksDiff
// 100 nanosecond ticks: 10,000,000 ticks per second?
double DiffFactorSeconds = DiffFactorTicks/10000000;
double DiffFactorHours = DiffFactorSeconds/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 23720
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.ToLocalTime();
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.ToLocalTime();
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.Globalization.DaylightTime. 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
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...
0
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...
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...
4
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...
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...
1
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? ...
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 =...
2
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? ...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.