Skip to content

Instantly share code, notes, and snippets.

@alexisbernard
Created September 2, 2009 13:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexisbernard/179712 to your computer and use it in GitHub Desktop.
Save alexisbernard/179712 to your computer and use it in GitHub Desktop.
Parse date with I18n
# File: config/initializers/date.rb
# Parse date using Rails I18n or Ruby parse method if it failed.
class Date
class << self
def _parse_with_i18n(str, format = :default)
format ||= :default
date = Date._strptime(str, I18n.translate("date.formats.#{format}")) || _parse_without_i18n(str)
date[:year] += increment_year(date[:year].to_i) if date[:year]
date
end
alias_method_chain :_parse, :i18n
def parse_with_i18n(str, format = :default)
format ||= :default
date = Date.strptime(str, I18n.translate("date.formats.#{format}"))
Date.new(date.year + increment_year(date.year), date.month, date.day)
rescue ArgumentError
parse_without_i18n(str)
end
alias_method_chain :parse, :i18n
def increment_year(year)
if year < 100
year < 30 ? 2000 : 1900
else
0
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment