BT

InfoQ Homepage News New Best Practices for Working with Date/Time Values

New Best Practices for Working with Date/Time Values

Bookmarks

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.

Rate this Article

Adoption
Style

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.

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

Community comments

  • Not a second too late

    by Eirik Mangseth /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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 /

    Your message is awaiting moderation. Thank you for participating in the discussion.

    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

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

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.