Skip to content

Instantly share code, notes, and snippets.

@johnsome
Created March 17, 2011 01:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnsome/873699 to your computer and use it in GitHub Desktop.
Save johnsome/873699 to your computer and use it in GitHub Desktop.
ruby version date parsing differences and options
Key issue - format of entered dates:
date is displayed like: 31/3/2011
date is input like: 31-3-2011
This is works correctly in ruby 1.8 and 1.9
ruby-1.8.7-p302 > Time.parse '31-3-2011' # => Thu Mar 31 00:00:00 -1000 2011
ruby-1.9.2-p0 > Time.parse('31-3-2011') # => 2011-03-31 00:00:00 -1000
Changes in parsing dates for ruby 1.9:
* no longer has difference in parsing dates with dash or forward slash - they are always in euro format
potential for wrong dates:
ruby-1.8.7-p302 > Time.parse('10/1/2011') # => Sat Oct 01 00:00:00 -1000 2011
ruby-1.8.7-p302 > Time.parse('10-1-2011') # => Mon Jan 10 00:00:00 -1000 2011
ruby-1.9.2-p0 > Time.parse('10/1/2011') # => 2011-01-10 00:00:00 -1000
ruby-1.9.2-p0 > Time.parse('10-1-2011') # => 2011-01-10 00:00:00 -1000
or rasing exceptions:
ruby-1.8.7-p302 > Time.parse('3/31/2011') # => Thu Mar 31 00:00:00 -1000 2011
ruby-1.9.2-p0 > Time.parse('3/31/2011')
ArgumentError: argument out of range
from /Users/mark/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/time.rb:198:in `local'
from /Users/mark/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/time.rb:198:in `make_time'
from /Users/mark/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/time.rb:267:in `parse'
from (irb):4
from /Users/mark/.rvm/rubies/ruby-1.9.2-p0/bin/irb:17:in `<main>'
and in euro format:
ruby-1.8.7-p302 > Time.parse '31/3/2011'
ArgumentError: argument out of range
from /Users/mark/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/time.rb:184:in `local'
from /Users/mark/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/time.rb:184:in `make_time'
from /Users/mark/.rvm/rubies/ruby-1.8.7-p302/lib/ruby/1.8/time.rb:243:in `parse'
from (irb):60
ruby-1.9.2-p0 > Time.parse '31/3/2011' # => 2011-03-31 00:00:00 -1000
ISO standard format:
ruby-1.8.7-p302 > Time.parse('2011-3-31') # => Thu Mar 31 00:00:00 -1000 2011
ruby-1.9.2-p0 > Time.parse('2011-3-31') # => 2011-03-31 00:00:00 -1000
ruby-1.8.7-p302 > Time.parse('2011/3/31') # => Thu Mar 31 00:00:00 -1000 2011
ruby-1.9.2-p0 > Time.parse('2011/3/31') # => 2011-03-31 00:00:00 -1000
==
So if we want to have users input in euro format, i.e. 31/3/2011, and support Ruby 1.9,
then we need to probably use something like delocalize gem https://github.com/clemens/delocalize
OR:
we just use ISO standard (YYYY-MM-DD) for all data entry related to dates, and not worry about it.
@zdavatz
Copy link

zdavatz commented Oct 12, 2011

Thanks for sharing this! Is this valid for Ruby 1.9.3-RC1 as well?

Best
Zeno

@johnsome
Copy link
Author

Hi Zeno -
I haven't looked at 1.9.3, so no idea if it's changed - let me know if you find anything different.

Thanks!
Mark

@zdavatz
Copy link

zdavatz commented Oct 14, 2011

Thanks, seems that we solved the problem internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment