Jacob Gorban wrote:
Hi,
For some reason ruby 1.8.6 return invalid date exception when parsing a
date of Oct 31 (yesterday) in the format above. Oct 30 and most other
dates parses fine and run fine. Time, year or timezone don't matter.
I doubt it "parses fine" by your definition:
Date.parse('17:26:33 Oct 30, 2009').to_s
=> "2009-11-30"
If you want to see why, the source to date.rb is there. The heavy
lifting is in Date._parse from date/format.rb, which you can walk
through by hand easily enough.
require 'date'
=> true
str = '17:26:33 Oct 30, 2009'
=> "17:26:33 Oct 30, 2009"
e = Date::Format::Bag.new
=> #<Date::Format::Bag:0xb7b2daec @elem={}>
str.gsub!(/[^-+',.\/:0-9@a-z\x80-\xff]+/in, ' ')
=> "17:26:33 Oct 30, 2009"
Date.class_eval { _parse_time(str, e) }
=> true
Date.class_eval { _parse_day(str, e) }
=> nil
e.to_hash
=> {:zone=>"Oct", :sec=>33, :hour=>17, :min=>26}
str
=> " 30, 2009"
Date.class_eval {
?> _parse_eu(str, e) ||
?> _parse_us(str, e) ||
?> _parse_iso(str, e) ||
?> _parse_jis(str, e) ||
?> _parse_vms(str, e) ||
?> _parse_sla_us(str, e) ||
?> _parse_iso2(str, e) ||
?> _parse_year(str, e) ||
?> _parse_mon(str, e) ||
?> _parse_mday(str, e) ||
?> _parse_ddd(str, e)
}
=> true
e.to_hash
=> {:zone=>"Oct", :sec=>33, :hour=>17, :mday=>30, :min=>26}
So you can see that "Oct" is taken as the timezone, and there is no
month. I expect it's subsequently defaulting the month to the current
month (Nov), and that's why you're not allowed the 31st.
HTH,
Brian.
···
--
Posted via http://www.ruby-forum.com/\.