It becomes necessary to deal with dates and times in most .NET programs. A lot of programs use DateTime but that structure is frought with potential issues when you start serializing, parsing, comparing and displaying dates from different time zones and cultures. In this post I will go over these issues and the APIs and practices you should use to avoid them.
The DateTime structure stores only two pieces of information: Ticks and Kind. 1 tick is 100 nanoseconds (10,000 ticks in a millisecond). The ticks counter represents how many 100ns ticks you are away from 1/1/0001 12:00 AM. It’s the part that determines that it’s April 6th, 2012, 3:32:07 PM.
Another bit of data is the Kind: represented as a DateTimeKind enumeration. Utc, Local and Unspecified are the possible values. Utc means that the ticks counter represents a Coordinated Universal Time (that doesn’t change due to daylight savings or time zones). Local means that it represents the local time of whatever time zone the computer is set to. This one is sensitive to daylight savings. Note that a timezone offset is not built in to the DateTime structure. It can only get back to a real UTC time by checking the current time zone settings on the computer. ToUniversalTime() and ToLocalTime() will do these conversions. It will return a new DateTime value with the Ticks adjusted to get the correct year/month/day and with the requested type. Calling ToUniversalTime() on a UTC DateTime or ToLocalTime() on a Local DateTime has no effect. Remember that these functions are generating a new DateTime value and not modifying the original.
There’s also a third type: Unspecified. This means we don’t know whether it’s local or UTC. All we know is the year, month, day, etc. When working with DateTime values, Unspecified is not very helpful since we don’t know what to do when we want to get a local or UTC time from it. If you call ToUniversalTime() on one of these, it assumes that the type must have been Local and converts based on that. If you call ToLocalTime() it assumes that the type must have been UTC and converts in that direction.
DateTimeOffset is a newer structure. It is also based on ticks but instead of storing a Kind, it keeps track of the offset from UTC as a TimeSpan. A DateTimeOffset always knows what time zone it’s in. Calling ToUniversalTime() will always result in a TimeSpan.Zero offset, and ToLocalTime() will convert and result in an offset of the user’s current time zone.
Use DateTimeOffset, not DateTime
DateTime has countless traps in it that are likely to give your code bugs:
- DateTime values with DateTimeKind.Unspecified are bad news. It’s common for code to call ToUniversalTime() or ToLocalTime() to make sure it’s got the date properly ready for storage or display respectively. If you call ToUniversalTime() it converts X hours in one direction, but if you call ToLocalTime() on it, it converts X hours in the opposite direction. Both of those can’t possibly be correct.
- DateTime doesn’t care about UTC/Local when doing comparisons.
But DateTimeOffset doesn’t have any of these problems! It’s the newer, more robust version; think of it as DateTime v2. They couldn’t fix the old DateTime object because that might break existing code that was relying on the bad behavior.
So you’ll want to use DateTimeOffset in any situation that you’re referring to a specific point in time. You might still use DateTime for things like general dates (July 4th, 1776) or store hours (9AM-5PM), since they are not affected by time zones.
Pick the right format when serializing
|The only format that preserves the DateTimeOffset fully. The others round to the nearest second.|
|“r”||RFC1123||Tue, 17 Apr 2012 16:46:48 GMT||Used for HTTP headers|
|“u”||Universal sortable||2012-04-17 16:46:48Z|
|Not a great format for this use as it has no timezone or UTC marker and can change based on what offset you have|