tags:

views:

6029

answers:

4

Hi - I am working in C#.net - .Net fx is 2.0 which doesnot support converting between different time zones. I have wrote a scheduler based on UTC but it is giving errors of 1 hour in the DTS periods for London. I need some solution so that I can gat the correct time in any timezone relative to UTC with correct DST adjustments.

A: 

.NET does support converting UTCtime to local time:

DateTime currentServerTime = DateTime.UtcNow.ToLocalTime();

See here for more info.

Mitch Wheat
That supports a single timezone - the one the code is running on. It's no good for arbitrary timezones. To be honest I'm amazed that MS got away with this situation for so long.
Jon Skeet
+4  A: 

Is changing to .NET 3.5 absolutely out of the question? It would make your life much, much easier. Otherwise, you're stuck with the plain TimeZone and DaylightSavings classes, as well as having to fetch the known timezones using P/Invoke.

William Stacey has a blog post with some code to do this - but I haven't tried it, so can't vouch for its accuracy. (In my experience he's usually pretty good though :) There are no doubt similar bits of code around if that one doesn't help you.

I believe that the API he's using doesn't have access to historical data, btw. In other words, it will assume that DST always kicks in on the first Sunday of October (or whatever the rule is) rather than knowing that the rule has changed over time. TimeZoneInfo in .NET 3.5 supports historical data where the OS does.

Jon Skeet
Agree. I've seen old code doing the P/Invoke stuff, and TimeZoneInfo in 3.5 renders it all obsolete.
Robert Paulson
@Jon I have finally managed to have the platform changed to 3.5.
MSIL
A: 

yep I cannot switch to .Net 3.5 - I know this is very easy in 3.5

MSIL
A: 

DateTime currentServerTime = DateTime.UtcNow.ToLocalTime(); only changes based on the current offset.

Date & Times from the past won't be accurate because of Daylight Savings.

In other words, If you save a date during daylight savings, then read it when daylight isn't in effect it will not be the same time.

I'm looking for a simple solution for this, if I one I'll post it.