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

R

Randall Parker

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?
 
J

Joshua Flanagan

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/d.../frlrfsystemdatetimeclasstolocaltimetopic.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
 
R

Randall Parker

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.
 
J

Joshua Flanagan

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top