Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a full implementation for ISO-8601 date parsing for Ruby?

The Time.iso8601 method is a restricted subset of ISO-8601.

  • What are its limitations?
  • Does anyone know of a full implementation for Ruby? I'm using MRI 1.8.7.

Update

It looks like there isn't a single class that handles all of the various 8601 date and date/time combinations. However, I have managed to work around the problems by using both the Date.parse and Time.iso8601 methods. The downside is that you need to decide in code whether the input looks like a date or a date/time.

Warning : Timezone differences

Time.iso8601 and Time.parse behave differently.

>> Time.parse("2010-09-06T12:27:00.10-05:00") => Mon Sep 06 18:27:00 +0100 2010  >> Time.iso8601("2010-09-06T12:27:00.10-05:00") => Mon Sep 06 17:27:00 UTC 2010 

Differences between Time.iso8601 and ISO-8601

This document touches on the differences between what is in ISO-8601 and what is supported by Ruby. The short answer is that the number of possible formats is restricted.

like image 963
Chris McCauley Avatar asked Sep 03 '10 11:09

Chris McCauley


People also ask

How do I format a date in ISO 8601?

ISO 8601 represents date and time by starting with the year, followed by the month, the day, the hour, the minutes, seconds and milliseconds. For example, 2020-07-10 15:00:00.000, represents the 10th of July 2020 at 3 p.m. (in local time as there is no time zone offset specified—more on that below).

How do you parse time in Ruby?

Ruby | DateTime parse() function DateTime#parse() : parse() is a DateTime class method which parses the given representation of date and time, and creates a DateTime object. Return: given representation of date and time, and creates a DateTime object.

Is ISO 8601 always UTC?

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long ( YYYY-MM-DDTHH:mm:ss. sssZ or ±YYYYYY-MM-DDTHH:mm:ss. sssZ , respectively). The timezone is always zero UTC offset, as denoted by the suffix Z .


2 Answers

Yes, but unfortunately it's in Ruby 1.9.

require "date"  Date.iso8601("2010-W32-5").strftime #=> "2010-08-13"  

I don't believe there are any implementations for Ruby 1.8.7 (or at least I couldn't find any). You could either try to upgrade to Ruby 1.9, which is pretty stable as of 1.9.2. Alternatively, you could try to parse the dates yourself.

like image 175
molf Avatar answered Sep 23 '22 23:09

molf


To convert an ISO8601 date into the local time zone, do this:

require "time" dt1 = Time.parse("2010-09-06T12:27:00.10-05:00") 

To convert an ISO8601 date into UTC, do this:

dt2 = Time.iso8601("2010-09-06T12:27:00.10-05:00") 

If you compare the dates returned by the above queries, they will be identical (i.e. dt1 === dt2). However, accessing date components (like year, month, day, hour, etc.) will return values appropriate for the time zone (either UTC or local). The same applies to strftime.

like image 45
Pete Avatar answered Sep 21 '22 23:09

Pete