BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

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

New Best Practices for Working with Date/Time Values

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

BT