Skip to content

Instantly share code, notes, and snippets.

@clemens
Created July 30, 2008 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clemens/3351 to your computer and use it in GitHub Desktop.
Save clemens/3351 to your computer and use it in GitHub Desktop.
[11:21pm] clemensk: my idea is to pretty much remove the DATE_FORMATS constants and deprecate them
[11:21pm] clemensk: like it has been done with the COUNTRIES in the form options helper
[11:21pm] clemensk: and instead have a DEFAULT_FORMATS constant
[11:22pm] clemensk: that holds all the formats that are used internally by rails
[11:22pm] clemensk: like :db, :number and :rfc822
[11:22pm] clemensk: assume someone calles Time.now.to_s(:long)
[11:22pm] clemensk: this would first do a lookup in DEFAULT_FORMATS
[11:23pm] clemensk: and then, if it doesn't find it there, it'd do I18n.localize(self, format)
[11:23pm] clemensk: and if it's not stored in the locale either, it falls back to to_default_s
[11:23pm] svenfuchs: hi peeps
[11:24pm] clemensk: apart from the i18n integration, this also has the benefit that the user doesn't accidentally overwrite formats that are used internally by rails
[11:26pm] clemensk: currently:
[11:26pm] clemensk: def to_formatted_s(format = :default)
[11:26pm] clemensk: return to_default_s unless formatter = DATE_FORMATS[format]
[11:26pm] clemensk: formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter)
[11:26pm] clemensk: end
[11:26pm] clemensk: how I'd like it:
[11:26pm] clemensk: DATE_FORMATS.keys.include?(format) ? strftime(DATE_FORMATS[format]) : I18n.localize(self, format) rescue to_default_s
[11:26pm] clemensk: or rather DEFALT_FORMATS (sorry)
[11:26pm] clemensk: +U
[11:27pm] tsaleh|away is now known as tsaleh.
[11:28pm] clemensk: with the i18n functionality, this even gives us the possibility to directly use a strftime string ... like Time.now.to_s("%d.%m.%Y")
[11:28pm] clemensk: which would be localized correctly
def localize(locale, object, format = :default)
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
if format.is_a? Symbol
type = object.respond_to?(:sec) ? 'time' : 'date'
format = translate(locale, :"#{type}.formats.#{format}") # raises I18n::MissingTranslationData if format isn't found
end
format = format.to_s
format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
object.strftime(format)
end
# currently:
def to_formatted_s(format = :default)
return strftime(DATE_FORMATS[format]) if DATE_FORMATS.keys.include?(format)
type = self.respond_to?(:sec) ? 'time' : 'date' # could be TimeWithZone so I need to even check that in date
# neet to check first if the format is defined - if not, fall back to default to_s
I18n.translate(:"#{type}.formats.#{format.to_s}") ? I18n.localize(self, format) : to_default_s
end
def to_formatted_s(format = :default)
# fall back to default to_s if localize raises an exception
DATE_FORMATS.keys.include?(format) ? strftime(DATE_FORMATS[format]) : I18n.localize(self, format) rescue to_default_s
end
# with proc
def localize(locale, object, format = :default)
raise ArgumentError, "Object must be a Date, DateTime or Time object. #{object.inspect} given." unless object.respond_to?(:strftime)
format = case format
when Symbol
type = object.respond_to?(:sec) ? 'time' : 'date'
translate(locale, :"#{type}.formats.#{format}") # raises I18n::MissingTranslationData if format isn't found
when Proc
# allows for something like :long_ordinal => lambda { |date| "%B #{date.day.ordinalize}, %Y" }, # => "April 25th, 2007"
# similar to current Rails functionality, see ActiveSupport::CoreExtensions::Date
format.call(object).to_s
else
format.to_s
end
format.gsub!(/%a/, translate(locale, :"date.abbr_day_names")[object.wday])
format.gsub!(/%A/, translate(locale, :"date.day_names")[object.wday])
format.gsub!(/%b/, translate(locale, :"date.abbr_month_names")[object.mon])
format.gsub!(/%B/, translate(locale, :"date.month_names")[object.mon])
format.gsub!(/%p/, translate(locale, :"time.#{object.hour < 12 ? :am : :pm}")) if object.respond_to? :hour
object.strftime(format)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment