Skip to content

Instantly share code, notes, and snippets.

@troyk
Created April 15, 2011 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save troyk/922048 to your computer and use it in GitHub Desktop.
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
# 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
@kfprimm
Copy link

kfprimm commented Oct 19, 2011

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.

@troyk
Copy link
Author

troyk commented Oct 19, 2011

Correct, thanks for the heads up

@bbhoss
Copy link

bbhoss commented Apr 29, 2012

Any idea why this doesn't work with Ruby 1.9.3? euro_parse gets defined, but _parse doesn't get overridden.

@troyk
Copy link
Author

troyk commented Nov 15, 2013

@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