BT

New Best Practices for Working with Date/Time Values

by Jonathan Allen on Jun 18, 2007 |

A common problem with programming languages, including those of .NET, is the lack of decent time zone support. In order to properly support them, programs have to either carry around the time zone as a separate field or carefully convert them to and from a canonical value. Too often developers pretend that time zones do not exist at all rather than go through this level of effort.

Microsoft seeks to change this for .NET programmers by introducing two new classes. The first class is TimeZoneInfo. This provides deeper support for time zone information than the older TimeZone class, including the ability to enumerate time zone information stored at the OS level.

The second class being added is the one that will really change things. Called DateTimeOffset, this class replaces DateTime under most circumstances. Unlike DateTime, which can only refer to the local and UTC time zones, DateTimeOffset can be tagged with any time zone as an offset from UTC.

According to Anthony Moore, DateTimeOffset should be considered the default for most circumstances involving time. The exceptions are…

Use DateTime for any cases where the absolute point in time does not apply: e.g. store opening times that apply across time zones.
Use DateTime for interop scenarios where the information is a Date and Time without information about the time zone, e.g. OLE Automation, databases, existing .NET APIs that use DateTime, etc.
Use DateTime with a 00:00:00 time component to represent whole dates, e.g. Date of birth.
Use TimeSpan to represent times of day without a date.

According to Justin Van Patten, the Base Class Library (BCL) will be moving towards DateTimeOffset in the future, though not everyone is happy with this decision. Some are calling for an IDateTime interface to replace all DateTime signatures, while others want a DateTime class that only stores UTC time. 

It remains to be seen how these classes will play into the rest of the framework, especially with the technologies like ADO.NET and LINQ.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Not a second too late by Eirik Mangseth

If only M$ could learn from postgreSQL how to handle dates (e.g. in SQLServer) properly it would be a cause for celebration.

E.M.

UPDATE - History of DateTime in .NET by Jonathan Allen

Anthony Moore has posted a follow-up with the history of DateTime and more information on how and why the new classes were developed.

blogs.msdn.com/bclteam/archive/2007/06/18/a-bri...

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT