Class: IsoDoc::ExtendedDateFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/isodoc/extended_date.rb

Overview

Extended strftime-style date formatter on top of Ruby’s Date#strftime.

Adds three POSIX-flavoured surfaces:

%E[YyC](?:\{ARGS\})?  - era year (calendar-aware)
%O[mdYy](?:\{ARGS\})? - alternative numbering for date components
%_                    - legacy alias for a literal space (kept for
                        backwards compatibility with IsoDoc::I18n#date)

The localised name directives (%B, %b, %h, %A, %a, %P, %p) are also routed through the formatter so they pick up CLDR locale data without the previous ZWNJ-marker hack in IsoDoc::I18n.

ARGS is a comma-separated list of either a positional numbering-system name (numeric, spellout, hanidec, roman, roman-lower) or key=value pairs. The only key currently honoured is cal= (japanese|gregorian); other CLDR calendar identifiers (roc, buddhist, persian, islamic, indian, hebrew) are reserved as documented extension points and raise NotImplementedError today.

Constant Summary collapse

TOKEN_RX =
/
  %_                                  |
  %\^?[BbhPpAa]                       |
  %E[YyC](?:\{[^}]*\})?               |
  %O[mdYy](?:\{[^}]*\})?
/x.freeze
DAY_KEYS =
%i[sun mon tue wed thu fri sat].freeze
HANIDEC_FROM =
"0123456789".freeze
HANIDEC_TO =
"〇一二三四五六七八九".freeze
SUPPORTED_CALENDARS =
%i[gregorian japanese].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lang:, script: nil, calendar: nil, calendar_en: nil) ⇒ ExtendedDateFormatter

Returns a new instance of ExtendedDateFormatter.



44
45
46
47
48
49
# File 'lib/isodoc/extended_date.rb', line 44

def initialize(lang:, script: nil, calendar: nil, calendar_en: nil)
  @lang = lang.to_s
  @script = script
  @cal = calendar || twitter_cldr_calendar
  @cal_en = calendar_en || TwitterCldr::Shared::Calendar.new(:en)
end

Instance Attribute Details

#langObject (readonly)

Returns the value of attribute lang.



42
43
44
# File 'lib/isodoc/extended_date.rb', line 42

def lang
  @lang
end

#scriptObject (readonly)

Returns the value of attribute script.



42
43
44
# File 'lib/isodoc/extended_date.rb', line 42

def script
  @script
end

Class Method Details

.format(value, fmt, **opts) ⇒ Object



38
39
40
# File 'lib/isodoc/extended_date.rb', line 38

def self.format(value, fmt, **opts)
  new(**opts).format(value, fmt)
end

Instance Method Details

#format(value, fmt) ⇒ Object



51
52
53
54
# File 'lib/isodoc/extended_date.rb', line 51

def format(value, fmt)
  time = coerce(value)
  tokenise(fmt).map { |kind, payload| render(time, kind, payload) }.join
end