Class: Worldwide::Cldr::DateFormatPattern::YearField

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

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



150
151
152
153
154
155
156
157
# File 'lib/worldwide/cldr/date_format_pattern.rb', line 150

def format(date, locale: I18n.locale)
  if pattern.length == 2
    # Calendar year (numeric), final 2 digits (e.g., 2019 -> 19)
    date.year.abs.to_s[-2..]
  else # Calendar year (numeric), all digits, zero padded to the number of "y" characters (e.g., 2019)
    date.year.abs.to_s.rjust(pattern.length, "0")
  end
end