Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in date string parsing between Ruby 1.9.3 and Ruby 1.8.7

Tags:

date

time

ruby

With Ruby 1.8.7:

>> require 'time'
>> Time.parse '01/28/2012'
=> Sat Jan 28 00:00:00 +0200 2012
>> Time.parse '28/01/2012'
=> ArgumentError: argument out of range

With Ruby 1.9.3:

>> require 'time'
>> Time.parse '28/01/2012'
=> 2012-01-28 00:00:00 +0200
>> Time.parse '01/28/2012'
=> ArgumentError: argument out of range

It looks like in Ruby 1.8.7 it was accepting US format (month/day/year), while in Ruby 1.9.3 it accepts only non US format (day/month/year).

Is there a way to change this behavior to be like Ruby 1.8.7?

like image 574
arikfr Avatar asked Feb 25 '26 11:02

arikfr


1 Answers

Would it be an option for you to use Time.strptime("01/28/2012", "%m/%d/%Y") in place of Time.parse? That way you have better control over how Ruby is going to parse the date.

If not there are gems: (e.g. ruby-american_date) to make the Ruby 1.9 Time.parse behave like Ruby 1.8.7, but only use it if it's absolutely necessary.

1.9.3-p0 :002 > Time.parse '01/28/2012'
ArgumentError: argument out of range

1.9.3-p0 :003 > require 'american_date'
1.9.3-p0 :004 > Time.parse '01/28/2012'
 => 2012-01-28 00:00:00 +0000 
like image 83
pjumble Avatar answered Feb 27 '26 02:02

pjumble