Location is everything in a lot of things. The same is true of DateTime’s IsDaylightSavingTime method. This method uses the time zone setting of the machine it’s running on to determine this value.
What if you need to change that?
Be sure to read the comment on the answer!
Duh, of course it does! My words after I realized my mistake! You see, I live in Arizona and we don’t deal with the silliness of jumping forward and springing back our clocks.
Old Code / Working Code
I was asked to update a project that was very small but because it was so old it wasn’t using the proper security constructs my employer had put in place (after this system was written) and it also was written long before the age of mobile and as such looked and worked poorly on all those shiny mobile devices out there.
In the project was a curious bit of code that I instantly wanted to
destroy, I mean refactor:
Private Function retTimeZoneAdjuster() As Byte
Select Case Year(Now)
Case Is = 2005
If Now > #4/3/2005 2:00:00 AM# And Now < #10/30/2005 2:00:00 AM# Then
Case Is = 2006
If Now > #4/2/2006 2:00:00 AM# And Now < #10/29/2006 2:00:00 AM# Then
Case Is = 2007
If Now > #3/11/2007 2:00:00 AM# And Now < #11/4/2007 2:00:00 AM# Then
Case Is = 2008
If Now > #3/9/2008 2:00:00 AM# And Now < #11/2/2008 2:00:00 AM# Then
And it continued on through for several more years. The app has been around a while. The calling code was using this to add 1 or 2 hours to a very important timestamp.
I thought .Net must have something for this and I found DateTime’s IsDaylightSavingTime method.
I felt extremely accomplished when I turned the long statement into this concise bit:
private DateTime Timestamp()
var stamp = DateTime.Now;
return stamp.AddHours(stamp.IsDaylightSavingTime() ? 2 : 1);
And for quite a while the rewritten program worked without issue.
Trouble In Pride Land
This last Sunday at 2AM we sprung forward and my happy little program and my happy group of users became, well, not so happy.
The email alerting me to the issue, since this was such a small system that
we, I didn’t bother to put in logging capabilities to track the dreaded unhandled exception was very simple indeed. You could imagine my surprise. "WAT!!!!???? No longer working at all you say???? WAT!!!!????? I haven’t touched that system since we launched it WITHOUT ISSUE".
Tests you ask? Oh, I think you know the answer to that. This is a small project, what do I need to test I can hear myself saying.
I toiled on the ash heap for a bit and then it struck me. We just changed Daylight Savings Time (DST). Could that be it?
Of course I opened up my editor and…
Duh, of course it does! I paused for a moment before I realized I live in Arizona and so does my server and we don’t do the DST shuffle. Ooooppps!
If I live in Arizona then why do I have to deal with this? Well, this timestamp is passed around to another server not in Arizona.
The DateTime class will always use the local system’s time zone setting. You can’t override this. That’s where the TimeZoneInfo class comes in. With this class you can create a time zone and convert times with it and call IsDaylightSavingTime for that zone.
private DateTime Timestamp()
var mountainTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
var mountainTime = TimeZoneInfo.ConvertTime(DateTime.Now, mountainTimeZone);
return DateTime.Now.AddHours(mountainTimeZone.IsDaylightSavingTime(mountainTime) ? 2 : 1);
First, create a time zone that respects Daylight Savings Time (DST). Then convert the existing time (in my zone) to the new zone and then test if the time is DST in the new zone.
Hopefully, this will help someone who needs to be concerned about DST. At least, it seemed like a nice little tale to tell.