I'm using TimeZoneInfo.ConvertTime method to convert time from one to another.
While converting the Date Time 1/1/2006 2.00 AM from Perth to Sri Jeyawardenepura its converted to 1/31/2005 11.30pm
While converting the same time back (1/31/2005 11.30pm) from Sri Jeyawardenepura to Perth its converted to 1/1/2006 3.00 AM.
Why there is one hour difference in the Time Zone conversion?
Wow, this is a Double Whammy! I just stumbled across this post and wasn't going to post anything at all since it's so old and the OP didn't show any code. But then curiosity got the best of me so I checked it out.
Using just the .NET BCL:
Sure enough, there is the discrepancy that the OP described. At first I thought this must be due to some kind of DST issue, so I checked for Sri Lanka and Perth. While both had a transition in 2006, neither were anywhere close to it for this date. Still, I thought I should check using
DateTimeOffset
to avoid any ambiguity issues:And it's still off. You can see that it thinks the target time should be at
+09:00
, but Perth didn't switch to that until December 3rd 2006. In January it was clearly still+08:00
.So then I thought... Noda Time to the rescue!
First let's check using the same Windows .NET BCL time zones.
Hey, that seems like it fixed it, right? If so, that would mean that the problem isn't with the Windows time zone data, because Noda Time's BCL provider uses the exact same data. So there must be something actually defective in
TimeZoneInfo.ConvertTime
. There's Whammy #1.So just to check that it's all good and well, let's try the same thing with IANA TZDB data. It's known to be more accurate after all:
And there, my friends, is Whammy #2. Notice that the middle time is using a
+06:00
offset? I thought this was in error, but when I checked once more here it turns out that the TZDB data is correct. Sri Lanka was at+06:00
at that time. It didn't switch to+05:30
until April.So to recap the Whammys:
TimeZoneInfo.ConvertTime
function appears to be flawed."Sri Lanka Standard Time"
zone is incorrect.All the better to just use Noda Time and TZDB always!
UPDATE
Thanks to Jon Skeet for helping identify that the first problem is with the way that the
"W. Australia Standard Time"
zone is being interpreted by theTimeZoneInfo
class.I dug much deeper into the .NET Framework reference source code, and I believe this is happening in the private static method
TimeZoneInfo.GetIsDaylightSavingsFromUtc
. I believe that they are not taking into account that DST doesn't always start and stop in the same calendar year.In this case, they are applying the 2006 adjustment rule with the 2005 year, and getting an
endTime
of1/2/2005
before thestartTime
of12/4/2005
. They do attempt to reconcile that this should be in 2006 (by incorrectly adding a year), but they don't consider that data is in reversed order.This problem will probably show up for any time zones that start their DST in the winter (such as Australia), and it will show up in one form or another any time the transition rule changes - which it did in 2006.
I've raised an issue on Microsoft Connect here.
The "second whammy" I mentioned is just because the historical data for Sri Lanka doesn't exist in the Windows time zone registry keys.
You would have to post the specific code to be certain. There could be an issue e.g. with daylight time being applied by one conversion but not the other.
There are may nuances of timezone management. Suggest you review this Jon Skeet blog for a great overview.
It is in fact so tricky to correctly use the .NET time classes that Jon has undertaken a port of Joda-Time to .NET, called Noda Time.
It's worth seriously considering for any project that supports multiple time zones.
Have you considered Day light savings when converting times ? Refer following link and you will get your answer. The time displayed is absolutely correct
http://www.timeanddate.com/worldclock/timezone.html?n=196&syear=2000
Just to add a little more information to Matt's answer, it seems that the BCL is very confused about its own data for Perth. It seems to think there were two transitions around the end of 2005 - one at 4pm UTC, and one eight hours later.
Demo:
Results:
This is highly bizarre, and may be related to the Libyan time zone breakage - although that doesn't have two transitions, just one misplaced one.