Michael Guterl
4/15/2009 1:55:00 AM
On Tue, Apr 14, 2009 at 9:43 PM, Rob Biedenharn
<Rob@agileconsultingllc.com> wrote:
>
> On Apr 14, 2009, at 8:39 PM, Michael Guterl wrote:
>
>> In attempting to make a small library of mine 1.9 compatible I have
>> run across an inconsistency in date parsing.
>>
>> Ruby 1.8:
>>>>
>>>> puts Date.parse("4/12/2009")
>>
>> 2009-04-12
>>
>>>> puts Date.parse("4/30/2009")
>>
>> 2009-04-30
>>
>> Ruby 1.9:
>>>>
>>>> puts Date.parse("4/12/2009")
>>
>> 2009-12-04
>>
>>>> puts Date.parse("4/30/2009")
>>
>> ArgumentError: invalid date
>>
>> I am sure I am not the first person to run across this issue. =C2=A0What=
is
>> the recommended way for dealing with this?
>>
>> Best,
>> Michael Guterl
>>
>
>
> Probably with Date.strptime rather than Date.parse so you can specify a
> format. I'm not actually that surprised by the 04-12 v. 12-04 since the d=
ocs
> say that there are heuristics, but 4/30/2009 ought to be unambiguous as 2=
009
> can't be a month or a day and 30 can't be a month.
>
> $ macirb
> irb> require 'date'
> =3D> true
> irb> puts Date.parse("4/30/2009")
> ArgumentError: invalid date
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from
> /Library/Frameworks/MacRuby.framework/Versions/0.3/usr/lib/ruby/1.9.0/dat=
e.rb:1023:in
> `new_by_frags'
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from
> /Library/Frameworks/MacRuby.framework/Versions/0.3/usr/lib/ruby/1.9.0/dat=
e.rb:1067:in
> `parse'
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from (irb):2
> =C2=A0 =C2=A0 =C2=A0 =C2=A0from /usr/local/bin/macirb:12:in `<main>'
> irb> puts Date.strptime("4/30/2009", "%m/%d/%Y")
> 2009-04-30
> =3D> nil
> irb> RUBY_VERSION
> =3D> "1.9.0"
>
Thanks Rob! If you would have came to Cincinnati.rb tonight you could
have told me in person. :)
Michael Guterl