Time::parse in ruby 1.9 with mm/dd/yyyy

In ruby 1.8, for dates in format mm/dd/yyyy, I get this:

$ ruby -rparsedate -e ‘p ParseDate.parsedate(“09/01/2008”)’
[2008, 9, 1, nil, nil, nil, nil, nil]
$ ruby -rtime -e ‘p Time.parse(“9/1/2008”)’
Mon Sep 01 00:00:00 -0400 2008

but in 1.9, I get this:

$ ruby19 -rtime -e ‘p Time.parse(“9/1/2008”)’
2008-01-09 00:00:00 -0500

Of course 9/1 is ambiguous, but even for a non-ambiguous date, 1.9
actually gives an error:

$ ruby -rtime -e ‘p Time.parse(“09/15/2008”)’
Mon Sep 15 00:00:00 -0400 2008
$ ruby19 -rtime -e ‘p Time.parse(“09/15/2008”)’
/usr/local/lib/ruby/1.9.0/time.rb:184:in local': argument out of range (ArgumentError) from /usr/local/lib/ruby/1.9.0/time.rb:184:inmake_time’
from /usr/local/lib/ruby/1.9.0/time.rb:243:in parse' from -e:1:in

Is there any way I can easily tell Time::parse to favor mm/dd/yyyy
over dd/mm/yyyy? I can’t use strptime because I have no idea what
format the dates are in. Also, I don’t want to have to write code to
figure out the format first, since that would mean I’m essentially
writing my own Time::parse.


Oh, here is some output of Date::_parse directly:

$ ruby -w -rdate -e ‘p Date._parse(“12/31/75 21:28:35”, true)’
{:sec=>35, :year=>1975, :hour=>21, :mday=>31, :min=>28, :mon=>12}
$ ruby19 -w -rdate -e ‘p Date._parse(“12/31/75 21:28:35”, true)’
{:hour=>21, :min=>28, :sec=>35, :year=>2012, :mon=>31, :mday=>75}

month == 31? mday == 75? Really??


This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs