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

J

Jim Cain

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:in `make_time'
from /usr/local/lib/ruby/1.9.0/time.rb:243:in `parse'
from -e:1:in `<main>'

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.

Cheers,
Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top