Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does DateTimeOffset deal with daylight saving time?

I am storing schedules in the database as a day of the week, hour and minute. When the data is read we create a DateTime object for the next occurrence of that day, hour and minute, but I need to modify this to be DST-aware. I am able to modify the database if necessary.

I know that DateTimeOffset stores a UTC date/time and an offset. I also know from this MSDN blog entry that DateTimeOffset should be used to "Work with daylight saving times".

What I'm struggling to understand is exactly how DateTimeOffset "work(s) with daylight saving times". My understanding, little that there is, is that daylight saving times are a political decision and cannot be inferred from purely an offset. How can it be that this structure is DST friendly if it only stores an offset and not a named timezone or country?

like image 594
Stephen Kennedy Avatar asked Sep 26 '11 10:09

Stephen Kennedy


People also ask

Does DateTimeOffset have timezone?

A DateTimeOffset value isn't tied to a particular time zone, but can originate from a variety of time zones. The following example lists the time zones to which a number of DateTimeOffset values (including a local Pacific Standard Time) can belong.

Does UTC offset change with daylight savings?

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.

Do we really benefit from daylight savings time?

Safety is one of the more solid arguments for keeping the lighter evenings of DST. Studies have found that DST contributes to improved road safety by reducing pedestrian fatalities by 13% during dawn and dusk hours. Another study found a 7% decrease in robberies following the spring shift to DST.

What is the big deal about daylight savings time?

The main purpose of Daylight Saving Time (called "Summer Time" in many places in the world) is to make better use of daylight. We change our clocks during the summer months to move an hour of daylight from the morning to the evening.


1 Answers

DateTimeOffset itself isn't really DST-aware, but TimeZoneInfo is. A DateTimeOffset represents a fixed instant in time - so you get to a DateTimeOffset via something that is time zone aware. In other words, if I asked for a DateTimeOffset now in the UK, I'd end up with something with an offset of +1 hour from UTC. If I asked for a DateTimeOffset for some time in December in the UK, I'd end up with something with an offset of 0 hours from UTC.

If you change your database to include the offset and you create the DateTimeOffset from the user's chosen DateTime (which should be of kind "unspecified") and their time zone, then that should give you the correct offset taking DST into account.

One thing to be aware of though: if I schedule something now for "2 years time" and you determine the offset now, that offset may not be correct in the future - for example, the government could change when DST applies, and obviously that's not going to change what's stored in your database.

like image 103
Jon Skeet Avatar answered Oct 20 '22 00:10

Jon Skeet