tags:

views:

192

answers:

4

I guess there are two major approaches to date representations in XML:

<date>1984-10-27</date>

and

<date>
  <year>1984</year>
  <month>10</month>
  <day>27</day>
</date>

Personally I would go for the former. It's more compact and at the same time more readable. The split-up of the second form seems overkill to me; e.g. most of the time the month in itself will have no information value.

Also you can't validate an element like <day> independently: you can define a range of [1..31], but 31 isn't acceptable if <month> is 2.

On the other hand the first form could cause confusion, especially if you use (FFF) MM-DD-YYYY or DD-MM-YYYY. I always use ISO 8601 format (YYYY-MM-DD), as it avoids this confusion.

Which XML code do you prefer?

+6  A: 

Practical experience has taught me that the easiest date values to work with is seconds-since-epoch. This value is portable across all languages and is timezone insensitive (your local date libraries will handle it).

For better or worse, XML tends to prefer more verbose and human readable values. If you want to use a human readable date value, I would recommend using the standard time format described by ISO 8601: http://www.w3.org/TR/NOTE-datetime. This is an unambiguous and internationally understood format. Parsers exist for various languages and it is generally the best textual representation of date values.

lucas-insasho
A: 

I vote for broken up. Whats wrong with the first you ask?

  • is that day/month or month/day ...errors are likely both human and machine
  • needs to be tokenized to get out the date information
  • you are storing formatting(separator) characters
Paxic
Your first and third point don't make sense: If the ISO format is used (and it should be for data exchange) then the order of the date components is clear, and the punctuation is part of the format and therefore mandatory.
mghie
Actually, the separators are not required by ISO 8601, but may be added for readability. Both YYYYMMDD and YYYY-MM-DD agree with ISO 8601.
stevenvh
I agree that a standard like ISO is the best practice to guarantee interoperability. I just like having the information already in atoms.
Paxic
+3  A: 

The standard for dates/times in xml is much more formal than that; most xml parsers will respect (for example):

  • 2009-02-15
  • 2009-02-15T10:20:34.0000000
  • 2009-02-15T10:20:34.0000000+00:00

This is always yyyy-MM-dd; the time portion is optional, as is the UTC offset.

(edit: lucas-insasho correctly cited the spec)

Marc Gravell
+2  A: 

While I agree with lucas-insasho that seconds-since-epoch is generally the easiest date/time representation to work with, it's not generally the easiest one to work with in XML. It can't be usefully processed by XSLT 1.0, nor can it be validated using XML Schema.

You can of course represent data in XML however you want. But really the only reason to use XML in the first place (instead of a more lightweight serialization format like JSON or YAML) is to reap those benefits that standardization has to offer.

Robert Rossney