DateTime.ToLocalTime () in winter / summer

I am using DateTime.ToLocalTime() to convert dates from UTC to local time. My time zone is GMT + 1 (Belgrade, Budapest, Lubnya ...), it is set correctly in the Windows (XP) settings.

Last weekend in our time zone, we switched to winter time in summer time, which means that we canceled the local time by one hour.

As I can see, the ToLocalTime method behaves strangely from now on. When I use it to convert dates that are after this winter time change, it works great, like this:

 var utcDate2 = new DateTime(2011, 11, 2, 9, 0, 0,DateTimeKind.Utc); 

utcDate1.ToLocalTime() Value: 2011.11.02. 10:00:00 2011.11.02. 10:00:00 it is correct

Burt, when I want to convert the date before this change (for example, date from daylight saving time), it returns an incorrect value like this:

 var utcDate1 = new DateTime(2011, 10, 23, 9, 0, 0,DateTimeKind.Utc); 

utcDate2.ToLocalTime() Value: 2011.10.23. 10:00:00 2011.10.23. 10:00:00 is incorrect. It should be 2011.10.23. 11:00:00

What should I do to get the correct values? How to use ToLocalTime, which also regulate winter / summer time?

+8
c # datetime
source share
4 answers

http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

On Windows XP systems, the ToLocalTime method only recognizes the current configuration rule when switching from UTC to local time. As a result, conversions for periods prior to the current adjustment rule that has entered into force may not accurately reflect the difference between UTC and local time.

So, you have to find another way to understand this.

+6
source share

I assume that the time zone data on your system may be out of date - or this is due to the jsobo restriction mentioned.

One option that you might want to (carefully) pursue is to use my date / time API, Noda Time . This one has a conversion to / from DateTime so you can use DateTime elsewhere in your code, although obviously I believe your code will be clearer if you used Noda Time everywhere :)

Noda Time is not "v1.0-ready" yet, but mainly due to several missing features. Regardless of whether you want to risk an open source project other than v1.0 or not, of course, it is up to you, but I would be very happy to help with any problems that you have encountered. (I'm really trying to find real use cases, so if there are any missing features that you need, I can very well implement them in anticipation of others who need the same.)

Noda Time uses the zoneinfo time zone database rather than the built-in Windows, so it should not have the same problems.

To check, you should use this code:

 DateTimeZone belgradeZone = DateTimeZone.ForId("Europe/Belgrade"); // Or whatever // Alternatively... DateTimeZone localZone = DateTimeZone.SystemDefault; ZonedDateTime utc = new ZonedDateTime(2011, 10, 23, 9, 0, 0, DateTimeZone.Utc); ZonedDateTime belgrade = new ZonedDateTime(utc.ToInstant(), belgradeZone); Console.WriteLine(belgrade.LocalDateTime); 
+1
source share

I use the same time zone, and when I try it, I get the correct values:

 var utcDate1 = new DateTime(2011, 10, 23, 9, 0, 0, DateTimeKind.Utc); Console.WriteLine(utcDate1); Console.WriteLine(utcDate1.ToLocalTime()); var utcDate2 = new DateTime(2011, 11, 2, 9, 0, 0, DateTimeKind.Utc); Console.WriteLine(utcDate2); Console.WriteLine(utcDate2.ToLocalTime()); 

Output:

 2011-10-23 09:00:00 2011-10-23 11:00:00 2011-11-02 09:00:00 2011-11-02 10:00:00 
+1
source share

.NET does some computing behind the scenes. I recommend reading this article by Raymond Chen about implicit time zone conversions.

0
source share

All Articles