ISO date format, XML and Zulu time

This has come up a few times, so I thought this was worth writing down. Like many things, there is one official way to write a date/time string. It’s called an “ISO date”, because it’s the ISO 8601 standard for how to write a date, time or both. It turns out specifically in XML files, lots of people make a DateTime field a string – and just put in any number of casual formats for a date. For example:
 
<?xml version="1.0" encoding="utf-8" ?>
<myData>
  <employees>
    <employee>
      <startDate>11/11/07 8:00am</startDate>
    </employee>
  </employees>
</myData>
 
This poses many problems. Many European countries format a date as dd/mm/yy – where the U.S. uses mm/dd/yy – and the above would fit both. Plus, it’s not a 4 digit year, which isn’t a problem in this case – but what if you are using the date to represent the original print date for a book or a painting? You have to use 4-digit years! Then on top of all that, what timezone was this referring too AND was it daylight savings at that time!? Daylight savings is no where near standard or observed in the same way/same dates.
 
As if that was enough, most times developers will define the above as a string – so they leave it to the consumer to parse the text and figure out if it’s a valid date and how to interpret it.
 
The ISO date format addresses this as it accounts for year, month, date, hour, minute, second, millisecond and timezone. For example:
 
2007-11-16T20:14:06.3Z                  (UTC/Zulu time)
2007-11-16T15:14:06.3-05:00        (A time at -5 hours from UTC, or “Eastern time”)
 
I am violently opposed to daylight savings, so to avoid me getting aggravated with dealing with it, I like to also store everything and refer to everything in Zulu time, that is the date/time at the Prime Meridian. Specifically, this is the time without application of time zones nor daylight savings. So the above string follows the ISO 8601 format and I convert to UTC (a.k.a. Zulu) time, then acknowledge that by specifying “Z” as the timezone offset. Time at the Prime Meridian is called Zulu (Z), Universal Time Coordinate (UTC) or Greenwich Mean Time (GMT).
 
So how would you affect this in (C#) code?
 
    /* ****************************
     * WRITING ISO FORMAT DATE/TIME
     * ****************************/
    // Get the current date/time
    DateTime currentTime = DateTime.Now;
 
    // Dump that out to a string
    string currentTimeString = currentTime.ToString();
 
    // Convert to UTC/Zulu, then output as ISO 8601
    string currentTimeIso = currentTime.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fZ");
 
 
    /* ***********************************************************
     * READING ISO FORMAT DATE/TIME (and converting to local time)
     * ***********************************************************/
    // An ISO 8601 date is absolutely parseable into a DateTime, no
    // FormatException or InvalidCastException will be thrown for any
    // variation of the ISO format. We also convert the UTC time to
    // local time (applying time zone and daylight savings)
    DateTime currentTimePost = DateTime.Parse(currentTimeIso).ToLocalTime();
 
    // Dump out the value to string
    string currentTimePostString = currentTimePost.ToString();
 
    // Show the starting date, then the post-processed string
    MessageBox.Show(currentTimeString + " – " + currentTimePostString);
 
So now, back to the XML. If you use ISO formatted dates, you can strongly-type your XML to xs:Date, and be able to natively read it right into a .NET DateTime variable. Here’s what that might look like:
 
<?xml version="1.0" encoding="utf-8" ?>
<myData>
  <employees>
    <employee>
      <startDate>2007-11-11T08:00:00.0Z</startDate>
    </employee>
  </employees>
</myData>
 
Now, there is no question as the format by a program or by a casual observer of that XML file. So the point here is, if you need to go out of the programming world into the arena of weakly-typed text files (even XML) – use ISO formatted dates!
 
Here is the rest of the spec for ISO 8601 dates: http://www.w3.org/TR/NOTE-datetime
Posted in General, Uncategorized
One comment on “ISO date format, XML and Zulu time
  1. ISO 9001 says:

    It was a awe-inspiring post and it has a significant meaning and thanks for sharing the information.Would love to read your next post too……

    Thanks

    Regards:
    ISO 9001

    Like

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: