ISO8601 is a standard for representing date/time information as a string. ISO8601 dates look like this:
- Unambiguous. There is never any question how to interpret them.
- Human-readable. You can look at dates stored in ISO8601 and interpret them easily.
- Widely-supported. All major programming languages have libraries for parsing and writing ISO8601 dates.
- Sortable. If you follow a few simple rules, ISO8601 dates sort lexicographically into the order you would expect.
That last property makes ISO8601 a particularly good candidate for date/time storage in simple non-relational data stores like Amazon’s SimpleDB. Storing dates as ISO8601 lets you sort records by date in data stores which don’t support a native date/time data type – without sacrificing human-readability.
Using ISO8601 from Ruby couldn’t be simpler, if you know where to look for the tools. If you look at the Ruby
Time class by itself, you won’t find any reference to ISO8601. The secret is to require the ‘time’ library:
irb(main):001:0> time = Time.now.utc.iso8601
NoMethodError: undefined method `iso8601' for Mon Oct 26 04:46:57 UTC 2009:Time
irb(main):002:0> require 'time'
irb(main):003:0> time = Time.now.utc.iso8601
=> Mon Oct 26 04:47:09 UTC 2009
Note the use of
utc to convert the local time into universal time (aka Greenwich Mean). This is important if you want your time strings to be lexicographically sortable. The “Z” on the end of the ISO8601 date indicates that it is in UTC, not a local time zone.