Created
April 15, 2011 17:07
-
-
Save troyk/922048 to your computer and use it in GitHub Desktop.
Date.parse() with Ruby 1.9 is now defaulting to the European date style, John Wayne would be sad, and our apps don't work right, so this fixes it
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Date.parse() with Ruby 1.9 is now defaulting to the European date style where the format is DD/MM/YYYY, not MM/DD/YYYY | |
# patch it to use US format by default | |
class Date | |
class << self | |
alias :euro_parse :_parse | |
def _parse(str,comp=false) | |
str = str.to_s.strip | |
if str == '' | |
{} | |
elsif str =~ /^(\d{1,2})[-\/](\d{1,2})[-\/](\d{2,4})/ | |
year,month,day = $3.to_i,$1,$2 | |
date,*rest = str.split(' ') | |
year += (year < 35 ? 2000 : 1900) if year < 100 | |
euro_parse("#{year}-#{month}-#{day} #{rest.join(' ')}",comp) | |
else | |
euro_parse(str,comp) | |
end | |
end | |
end | |
end |
Correct, thanks for the heads up
Any idea why this doesn't work with Ruby 1.9.3? euro_parse
gets defined, but _parse
doesn't get overridden.
@bbhoss my guess is another require/include is overwriting _parse without an alias after the euro_parse monkeys run
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this, saved me a lot of headaches! However, I believe _parse should return an empty hash instead of nil in the case of a blank string. Looks like that's how the original _parse functions as well.