API's that Suck

March 20, 2009

System.DateTime – Introducing the Ninja-bit

Filed under: Uncategorized — Grauenwolf @ 10:09 pm

Start with three variables, all are System.DateTime.

a: 10/2/2009 2:30:00 PM
b: 10/2/2009 2:30:00 PM
c: 10/2/2009 2:30:00 PM

Compare them to each other.

a=b: True
b=c: True
c=a: True

Ok, we have established that all three dates are equal. So when we convert them all to Universal time, we will get the same result. Right?

a.ToUniversalTime: 10/2/2009 9:30:00 PM
b.ToUniversalTime: 10/2/2009 9:30:00 PM
c.ToUniversalTime: 10/2/2009 2:30:00 PM


In .NET 2, System.DateTime was subtly and yet radically changed. Rather than adding full support for time zones, support for local and UTC time was hacked into two unused bits in the underlying data structure. These bits are the date’s “Kind” property.

a.Kind: Unspecified
b.Kind: Local
c.Kind: Utc

Why does this matter? Well, when you serialize a DateTime the Framework generally uses something like this:

a: 2009-10-02T14:30:00.0000000
b: 2009-10-02T14:30:00.0000000-07:00
c: 2009-10-02T14:30:00.0000000Z

So when your counter-party reads it back in, there are going to see two, maybe three, very different dates. And in case you are wondering, this did happen to me in a production scenario.


1 Comment »

  1. This could save me a lot of time in the future, thanks.

    Comment by Adam — March 30, 2009 @ 1:45 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: