Sunday, September 4, 2011

'Invalid date' error with Ruby 1.9.2

In one of my Rails 3.0 application, when migrated to Ruby 1.9.2, I am getting "invalid date" error sometimes.
Here is the detail error report:

invalid date
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:809:in `civil'
/Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/activesupport-3.0.5/lib/active_support/core_ext/string/conversions.rb:44:in `to_date'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:529:in `block (4 levels) in <top (required)>'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1768:in `each'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1202:in `block in foreach'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1340:in `open'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/csv.rb:1201:in `foreach'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:513:in `block (3 levels) in <top (required)>'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:504:in `each'
/Users/ror/projects/restaurant/lib/tasks/import_csv.rake:504:in `block (2 levels) in <top (required)>'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:205:in `call'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:205:in `block in execute'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:200:in `each'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:200:in `execute'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/task.rb:144:in `invoke'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:112:in `invoke_task'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `block (2 levels) in top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `each'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:90:in `block in top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:84:in `top_level'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:62:in `block in run'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:129:in `standard_exception_handling'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/lib/rake/application.rb:59:in `run'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2/bin/rake:32:in `<top (required)>'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/ror/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Tasks: TOP => import:csv

Some Observation
ruby-1.9.2-p290 :030 > a = '18/06/2011'
 => "18/06/2011"
ruby-1.9.2-p290 :031 > Date.parse(a)
 => Sat, 18 Jun 2011
ruby-1.9.2-p290 :032 > a = '06/18/2011'
 => "06/18/2011"
ruby-1.9.2-p290 :033 > Date.parse(a)
ArgumentError: invalid date
    from /Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:1022:in `new_by_frags'
    from /Users/ror/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/date.rb:1066:in `parse'
    from (irb):33
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
    from /Users/ror/projects/restaurant/highline/ruby/1.9.1/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Conclusion
Ruby 1.9.2 accepts date format "dd/mm/yyyy" not "mm/dd/yyyy"

No comments:

Post a Comment