469,338 Members | 8,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

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 23460
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by David Graham | last post: by
reply views Thread by Bill Borg | last post: by
5 posts views Thread by ECVerify.com | last post: by
1 post views Thread by XML newbie: Urgent pls help! | last post: by
1 post views Thread by Generic Usenet Account | last post: by
1 post views Thread by davelist | last post: by
2 posts views Thread by ZR | last post: by
1 post views Thread by Josef Dalcolmo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.