InfoQ

News

New Best Practices for Working with Date/Time Values

Posted by Jonathan Allen on Jun 18, 2007 06:23 AM

Community
.NET
Topics
Internationalization,
.NET Framework

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.

2 comments

Reply

Not a second too late by Eirik Mangseth Posted Jun 18, 2007 11:19 AM
UPDATE - History of DateTime in .NET by Jonathan Allen Posted Jun 18, 2007 11:27 AM
  1. Back to top

    Not a second too late

    Jun 18, 2007 11:19 AM 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.

  2. Back to top

    UPDATE - History of DateTime in .NET

    Jun 18, 2007 11:27 AM 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. http://blogs.msdn.com/bclteam/archive/2007/06/18/a-brief-history-of-datetime-anthony-moore.aspx

Exclusive Content

Measuring Agile in the Enterprise: 5 Success Factors for Large-Scale Agile Adoption

Michael Mah analyzes the development process in 5 companies: 2 Agile (one of them BMC) and 3 classic. He presents the factors which contributed to the success of BMC's Agile adoption.

Tom Preston-Werner on Powerset, GitHub, Ruby and Erlang

In this interview filmed at RubyFringe 2008, Tom Preston-Werner talks about how both Powerset and GitHub use Ruby and Erlang, as well as tools like Fuzed, god, and more.

David Laribee on Alt.NET and its Mission

David Laribee discusses the purpose of ALT.NET, its mission and future.

Discover RailsKits and Stop Writing Redundant Code

Ruby on Rails has become a popular Ruby framework for creating web applications in recent years. An aspect of creating a web application is the need to repeatedly create the same base functionality.

A Formal Performance Tuning Methodology: Wait-Based Tuning

Steven Haines talks about tackling web application performance tuning by proposing a method called wait-based tuning.

Shaw and Fowler About Forging a New Alliance

Shaw and Fowler talk about the need for a new relationship between the business department and the IT department. Studies have shown that projects mostly fail due to miscommunication between the two.

How to GET a Cup of Coffee

In this article, Jim Webber, Savas Parastatidis and Ian Robinson show how to drive an application's flow through the use of hypermedia in a RESTful application.

Archaeopteryx: A Ruby MIDI Generator

Eccentric artist turned overnight anti-celebrity, Giles Bowkett captures the heart and soul of RubyFringe as he demonstrates his revolutionary Archaeopteryx MIDI drum pattern generator.