Thursday, January 05, 2006 - Posts

ToUTC Times 2

Reminder to self… when setting a value on a structure that will eventually get xml serialized in .Net 1.x, as in a web service data contract, do not attempt to convert the local date to UTC before sending the message. .Net xml serialization will do this for you, so that the xml on the wire is always in UTC. Likewise, if you own the service on the other side of the wire that deserializes the message, the UTC will be converted to the local time of that server processing the message. If you make the mistake of converting to UTC before the message is serialized you end up with a date in the xml message that now contains both a time component that has already been adjusted for the GMT offset and the offset itself; in other words you end up doubling your offset impact.

I remember reading or hearing that this (datetime serialization) might be handled differently in .Net 2.0, but I just haven’t tried it yet. I think the plan was to do some bookkeeping in the object to know that it already represents UTC.