Class: Worldwide::Cldr::DateFormatPattern::EraField

Inherits:
Field
  • Object
show all
Defined in:
lib/worldwide/cldr/date_format_pattern.rb

Constant Summary collapse

GREGORIAN_COMMON_ERA_BOUNDARY =
Date.new(1, 1, 1)

Instance Attribute Summary

Attributes inherited from Field

#pattern

Instance Method Summary collapse

Methods inherited from Field

from, #initialize

Constructor Details

This class inherits a constructor from Worldwide::Cldr::DateFormatPattern::Field

Instance Method Details

#format(date, locale: I18n.locale) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/worldwide/cldr/date_format_pattern.rb', line 104

def format(date, locale: I18n.locale)
  era_number = date < GREGORIAN_COMMON_ERA_BOUNDARY ? 0 : 1
  case pattern.length
  when 1..3 # Era name, Abbreviated ("BC", "AD", "CE")
    Worldwide::Cldr.t("calendars.gregorian.eras.abbr.#{era_number}", locale: locale)
  when 4 # Era name, wide (e.g., "Anno Domini")
    Worldwide::Cldr.t("calendars.gregorian.eras.name.#{era_number}", locale: locale)
  when 5 # Era name, narrow (e.g., "A")
    Worldwide::Cldr.t("calendars.gregorian.eras.narrow.#{era_number}", locale: locale)
  else
    raise ArgumentError, "Invalid token: #{pattern}"
  end
end