.NET Does not Support Leap Seconds

by Warlock on Apr.13, 2008, under .NET

In my seemingly never ending quest to understand how accurately represent dates and times worldwide, I found out that .NET does not support leap seconds.  For those of you unfamiliar with the concept, “The Wikipedia” has a nice introduction.  Basically UTC uses an atomic clock to keep time (read: accurate) and the rest of us use a spinning ball circling another spinning ball to keep time (read: inaccurate).  The net result is we need to add or subtract a second here and there to keep things in line.  This is in addition to that whole day we throw into the mix every four years or so.

Anywhoo, this forum question raises some valid questions, so I did a quick check on my own and the following code does indeed throw an exception when it shouldn’t:

DateTime foo = new DateTime(2005, 12, 31, 23, 59, 60, DateTimeKind.Utc);

The MSDN documentation is mum on the issue, so I think it’s safe to say that this wasn’t a consideration.  It even specifically says that the second value will be on the range [0, 59].  This is just something to be aware of if you’re implementing a protocol that does support leap seconds, such as the iCalendar specification.

As an aside, this site is a good reference for all sorts of mistakes programmers make when working with dates & times.


Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...