Class: Worldwide::Cldr::DateFormatPattern

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

Defined Under Namespace

Classes: EraField, Field, InvalidPattern, Literal, QuarterField, YearField

Constant Summary collapse

FIELD_CLASSES =
{
  G: EraField,
  q: QuarterField,
  Q: QuarterField,
  y: YearField,
}

Class Method Summary collapse

Class Method Details

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

Populate a CLDR format string using a Date TODO: Implement all the date fields



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/worldwide/cldr/date_format_pattern.rb', line 13

def format(date, format, locale: I18n.locale)
  tokens = tokenize(format)

  formatted = tokens.map do |token|
    case token
    when Field
      token.format(date, locale: locale)
    when Literal
      token.value
    end
  end
  formatted.join("")
end

.tokenize(format) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/worldwide/cldr/date_format_pattern.rb', line 27

def tokenize(format)
  tokens = []
  i = 0
  while i < format.length
    token, length = parse_field(format[i..-1]) ||
      parse_single_quote(format[i..-1]) ||
      parse_quoted_literal(format[i..-1]) ||
      parse_unquoted_literal(format[i..-1])

    raise InvalidPattern, "Invalid token at index #{i}: #{format[i..5].inspect}" unless token

    tokens << token
    i += length
  end

  tokens
end