6

The next code shows wrong local time if current location is Moscow:

DateTime dt = new DateTime(2010, 1, 1, 10, 0, 0, 0, DateTimeKind.Utc);
Console.WriteLine(dt + " - " +  dt.ToLocalTime());

dt = new DateTime(2010, 7, 1, 10, 0, 0, 0, DateTimeKind.Utc);
Console.WriteLine(dt + " - " + dt.ToLocalTime());

Output:

01.01.2010 10:00:00 - 01.01.2010 14:00:00
01.07.2010 10:00:00 - 01.07.2010 15:00:00

It should be 13:00 and 14:00. How to fix it?

P.S. OS - Windows 7 Enterprise.

15
  • 1
    possible duplicate of C# - Convert UTC/GMT time to local time
    – bzlm
    Aug 8, 2013 at 10:45
  • 3
    Just as a side-note, I hear Nodatime is great with this sort of stuff, and a lot more accurate than DateTime. Maybe check it out.
    – JMK
    Aug 8, 2013 at 10:48
  • 3
    @judgeja: But not in January 2010. Back then it was UTC+3 Aug 8, 2013 at 10:48
  • 4
    @MatthewWatson is correct about the XP issue. The documentation of ToLocalTime explicitly states this: "On Windows XP systems, the ToLocalTime method recognizes only the current adjustment rule when converting from UTC to local time. As a result, conversions for periods before the current adjustment rule came into effect may not accurately reflect the difference between UTC and local time.". In other words: It uses the current rules, no matter whether they were in effect at the date you are converting. And that's exactly what you are seeing. Aug 8, 2013 at 10:52
  • 1
    @Rover: In my Windows 7 the results seem to be correct. However, you still might want to check out Noda Time as suggested by JMK. Aug 8, 2013 at 11:05

2 Answers 2

7

Looks like the reason is that when the new Russian laws have taken effect, Microsoft guys, instead of making Moscow time have permanent DST on, changed it's base UTC offset and disabled DST switching. Since timezone info does not contain any historic definitions of UTC offset (as opposed to historic DST switching), this broke the old datetime conversions. Hence, the results are as follows:

  • 01/01/2011 10:00:00 = 01/01/2011 14:00:00 because it's "GMT+4"
  • 01/07/2010 10:00:00 = 01/07/2010 15:00:00 because it's "GMT+4 + DST"

That's clearly an error in the Microsoft time library. At least they could have represented it as DST being permanently on....

Example code:

void Main()
{
    DateTime dt = new DateTime(2010, 1, 1, 10, 0, 0, 0, DateTimeKind.Utc);
    UTCToLocal(dt);

    dt = new DateTime(2010, 7, 1, 10, 0, 0, 0, DateTimeKind.Utc);
    UTCToLocal(dt);
}

void UTCToLocal(DateTime datetime) {
    if (datetime.Kind != DateTimeKind.Utc)
        throw new ApplicationException();

    Console.WriteLine("UTC: {0} MOW: {1} IsDST: {2}", datetime, datetime.ToLocalTime(),TimeZoneInfo.Local.IsDaylightSavingTime(datetime.ToLocalTime()));
}

Output:

UTC: 01/01/2010 10:00:00 MOW: 01/01/2010 14:00:00 IsDST: False
UTC: 01/07/2010 10:00:00 MOW: 01/07/2010 15:00:00 IsDST: True

You can clearly see that the summer datetime is being handled as DST one.

EDIT: The solution for the problem is in the next answer, I'll just leave mine here as a background.

3

This is a bug introduced into Windows historical timezone data. There is a hotfix available (this applies to Windows 7, Server 2008 R2 and others, not just XP).

Hotfix: Update for 2011 calendar history in Windows operating systems

As JMK said in the comments, you may want to consider the excellent library NodaTime instead, particularly if you are distributing your program to users who may not have this hotfix.

2
  • Yes, this bug (and the hotfix) are applicable to almost every supported version of Windows. The only exceptions I can see are Windows 8 and Server 2012. Aug 8, 2013 at 11:44
  • 1
    I can't install this update: "The update is not applicable to your computer." Language and OS is correct.
    – Rover
    Aug 9, 2013 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.