Date.parse 1.8 vs 1.9

M

Michael Guterl

In attempting to make a small library of mine 1.9 compatible I have
run across an inconsistency in date parsing.

Ruby 1.8:
2009-04-12
2009-04-30

Ruby 1.9:
2009-12-04
ArgumentError: invalid date

I am sure I am not the first person to run across this issue. What is
the recommended way for dealing with this?

Best,
Michael Guterl
 
R

Rob Biedenharn

In attempting to make a small library of mine 1.9 compatible I have
run across an inconsistency in date parsing.

Ruby 1.8:
2009-04-30

Ruby 1.9:
ArgumentError: invalid date

I am sure I am not the first person to run across this issue. What 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 docs say that there are heuristics, but 4/30/2009 ought to be
unambiguous as 2009 can't be a month or a day and 30 can't be a month.

$ macirb
irb> require 'date'
=> true
irb> puts Date.parse("4/30/2009")
ArgumentError: invalid date
from /Library/Frameworks/MacRuby.framework/Versions/0.3/usr/lib/ruby/
1.9.0/date.rb:1023:in `new_by_frags'
from /Library/Frameworks/MacRuby.framework/Versions/0.3/usr/lib/ruby/
1.9.0/date.rb:1067:in `parse'
from (irb):2
from /usr/local/bin/macirb:12:in `<main>'
irb> puts Date.strptime("4/30/2009", "%m/%d/%Y")
2009-04-30
=> nil
irb> RUBY_VERSION
=> "1.9.0"

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
M

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
 
R

Roger Pack

ArgumentError: invalid date

I am sure I am not the first person to run across this issue. What is
the recommended way for dealing with this?

http://redmine.ruby-lang.org/issues/show/634
treats to some background of it. But doesn't explain this oddity
[unless it is just Ruby saying "you are running into danger by even
attempting to parse dd/dd/dddd because the first two dd's could
theoretically later come back to bite you" [?]
 
B

botp

I am sure I am not the first person to run across this issue. =A0What is

fwiw, i prefer current ruby1.9 behaviour primarily because

Date.parse("2009/4/30") =3D=3D Date.parse("30/4/2009") #=3D>true

thus, i do not have this issue since i always enter dates strings in
yyyy/mm/dd or dd/mm/yyyy format

kind regards -botp
 
B

Brian Candler

Roger said:
http://redmine.ruby-lang.org/issues/show/634
treats to some background of it. But doesn't explain this oddity
[unless it is just Ruby saying "you are running into danger by even
attempting to parse dd/dd/dddd because the first two dd's could
theoretically later come back to bite you" [?]

It would be silly if 4/30/2009 were parsed as m=4,d=30,y=2009
but 4/5/2009 were parsed as d=4,m=5,y=2009
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top