Ray Kraft

Ray Kraft

  • NA
  • 1
  • 0

Round-tripping DateTimes within the 02:00-03:00 time range of DST fails

Mar 19 2009 1:59 PM
Can anyone tell me what round-tripping a DateTime within the 02:00-03:00 window of the daylight savings time switch fails?  This failure only occurs when run on a system where the automatic daylight savings time adjustment option of "Date and Time Properties" is enabled. 

Here is an example that I ran with automatic DST enabled, run with in the Pacific Time (US & Canada) time zone:

dt1 = new DateTime(2009, 3, 8, 2, 30, 0, 0, DateTimeKind.Local);
string s1 = dt1.ToString("o");
DateTime dt2 = DateTime.Parse(s1, null, DateTimeStyles.RoundtripKind);
dt2 = DateTime.SpecifyKind(dt2, DateTimeKind.Local);
Console.WriteLine("Original DateTime:      dt1: {0}", dt1.ToString("o");
Console.WriteLine("Round-tripped DateTime: dt2: {0}", dt2.ToString("o");


Which produces this output:

Original DateTime:           dt1: 2009-03-08T02:30:00.0000000-08:00
Round-tripped DateTime: dt2: 2009-03-08T03:30:00.0000000-07:00

The time strings differ in that the original DateTime did not properly recognize that it should have been in DST (-07:00).  My sense is that the dt1.ToString("o") function should have translated the input into DST (-07:00).  The Parse function certainly does.

Does anyone know how to successfully round-trip times specified within the DST switch range?

    -Ray Kraft