I want to compute the time 4 hours later during the DST transition, either into DST or out of it.
Currently doing this:
DateTime now = DateTime.now();
DateTime later = now.AddHours(4);
System.Console.WriteLine(now + " isDST " + now.IsDaylightSavingTime());
System.Console.WriteLine(later + " isDST " + later.IsDaylightSavingTime());
Provides:
11/1/2009 12:45:54 AM isDST True
11/1/2009 4:45:54 AM isDST False
What I want to see is:
11/1/2009 12:45:54 AM isDST True
11/1/2009 3:45:54 AM isDST False
The “best” way I’ve found to do this is to take a round trip through UTC like this:
DateTime now = DateTime.now();
DateTime later = now.ToUniversalTime().AddHours(4).ToLocalTime;
System.Console.WriteLine(now + " isDST " + now.IsDaylightSavingTime());
System.Console.WriteLine(later + " isDST " + later.IsDaylightSavingTime());
It seems I should be able to specify to AddHours() that the time returned should be elapsed time or display time. What am I missing?
I don’t think you’re missing anything. It sounds like DateTime could have a better documented or richer interface, but MSDN recommends using UTC exactly like you are.
There’s a cute time difference between .NET and win32 in displaying times from a different daylight savings’ rule than the present. .NET will display the time as it would have been displayed at that time, while win32 will always give it in terms of the current daylight savings flag.