Module: Russian::Strptime

Defined in:
lib/russian/strptime.rb

Overview

Localized strptime helpers for Russian month and weekday names.

The module normalizes Russian textual date/time tokens to the English tokens expected by Ruby’s native parsers, then delegates to Date.strptime, Time.strptime, or DateTime.strptime.

Локализованные хелперы strptime для русских названий месяцев и дней недели.

Модуль нормализует русские текстовые токены даты и времени к английским токенам, которые ожидают стандартные parser’ы Ruby, а затем делегирует работу в Date.strptime, Time.strptime или DateTime.strptime.

Class Method Summary collapse

Class Method Details

.abbr_day_names_key(format) ⇒ Object



162
163
164
# File 'lib/russian/strptime.rb', line 162

def abbr_day_names_key(format)
  Russian::LOCALIZE_STANDALONE_ABBR_DAY_NAMES_MATCH.match?(format) ? :"date.common_abbr_day_names" : :"date.standalone_abbr_day_names"
end

.abbr_month_names_key(format) ⇒ Object



152
153
154
# File 'lib/russian/strptime.rb', line 152

def abbr_month_names_key(format)
  Russian::LOCALIZE_ABBR_MONTH_NAMES_MATCH.match?(format) ? :"date.common_abbr_month_names" : :"date.standalone_abbr_month_names"
end

.date_strptime(string, format = "%F") ⇒ Date

Parses a localized Russian date string with Date.strptime.

Разбирает локализованную русскую строку даты через Date.strptime.

Parameters:

  • string (String)

    Localized date string. Локализованная строка даты.

  • format (String) (defaults to: "%F")

    Optional strptime format string. Строка формата strptime.

Returns:

  • (Date)

    Parsed date. Разобранная дата.



34
35
36
# File 'lib/russian/strptime.rb', line 34

def date_strptime(string, format = "%F")
  Date.strptime(normalize_input(string, format), format, Date::ITALY)
end

.datetime_strptime(string, format = "%FT%T%z") ⇒ DateTime

Parses a localized Russian date/time string with DateTime.strptime.

Разбирает локализованную русскую строку даты и времени через DateTime.strptime.

Parameters:

  • string (String)

    Localized date/time string. Локализованная строка даты и времени.

  • format (String) (defaults to: "%FT%T%z")

    Optional strptime format string. Строка формата strptime.

Returns:

  • (DateTime)

    Parsed datetime. Разобранный DateTime.



68
69
70
# File 'lib/russian/strptime.rb', line 68

def datetime_strptime(string, format = "%FT%T%z")
  DateTime.strptime(normalize_input(string, format), format, Date::ITALY)
end

.day_names_key(format) ⇒ Object



157
158
159
# File 'lib/russian/strptime.rb', line 157

def day_names_key(format)
  Russian::LOCALIZE_STANDALONE_DAY_NAMES_MATCH.match?(format) ? :"date.standalone_day_names" : :"date.common_day_names"
end

.directives_in(format) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/russian/strptime.rb', line 118

def directives_in(format)
  directives = []
  index = 0

  while index < format.length
    if format[index] != "%"
      index += 1
      next
    end

    if format[index + 1] == "%"
      index += 2
      next
    end

    index += 1
    index += 1 while index < format.length && Russian::STRPTIME_DIRECTIVE_MODIFIERS.include?(format[index])
    index += 1 while index < format.length && format[index].match?(/\d/)

    index += 1 if index < format.length && %w[E O].include?(format[index])

    directives << format[index] if index < format.length
    index += 1
  end

  directives
end

.localized_replacements(format) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/russian/strptime.rb', line 90

def localized_replacements(format)
  directives = directives_in(format)
  replacements = {}

  replacements.merge!(localized_tokens(month_names_key(format), Date::MONTHNAMES)) if directives.include?("B")

  if directives.include?("b")
    replacements.merge!(localized_tokens(abbr_month_names_key(format), Date::ABBR_MONTHNAMES))
  end

  replacements.merge!(localized_tokens(day_names_key(format), Date::DAYNAMES)) if directives.include?("A")

  replacements.merge!(localized_tokens(abbr_day_names_key(format), Date::ABBR_DAYNAMES)) if directives.include?("a")

  replacements
end

.localized_tokens(key, english_names) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/russian/strptime.rb', line 108

def localized_tokens(key, english_names)
  localized_names = I18n.t(key, locale: Russian::LOCALE).compact
  english_names = english_names.compact

  localized_names.zip(english_names).each_with_object({}) do |(localized_name, english_name), replacements|
    replacements[localized_name.downcase] = english_name
  end
end

.month_names_key(format) ⇒ Object



147
148
149
# File 'lib/russian/strptime.rb', line 147

def month_names_key(format)
  Russian::LOCALIZE_MONTH_NAMES_MATCH.match?(format) ? :"date.common_month_names" : :"date.standalone_month_names"
end

.normalize_input(string, format) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/russian/strptime.rb', line 73

def normalize_input(string, format)
  return string unless string.is_a?(String) && format.is_a?(String)

  replacements = localized_replacements(format)
  return string if replacements.empty?

  token_regexp = Regexp.new(
    "(?<!\\p{L})(?:#{replacements.keys.sort_by(&:length).reverse.map do |token|
      Regexp.escape(token)
    end.join("|")})(?!\\p{L})",
    Regexp::IGNORECASE
  )

  string.gsub(token_regexp) { |token| replacements.fetch(token.downcase) }
end

.time_strptime(string, format, now = Time.now) ⇒ Time

Parses a localized Russian date/time string with Time.strptime.

Разбирает локализованную русскую строку даты и времени через Time.strptime.

Parameters:

  • string (String)

    Localized date/time string. Локализованная строка даты и времени.

  • format (String)

    strptime format string. Строка формата strptime.

  • now (Time) (defaults to: Time.now)

    Optional base time passed to Time.strptime. Базовое значение времени, передаваемое в Time.strptime.

Returns:

  • (Time)

    Parsed time. Разобранное время.



52
53
54
# File 'lib/russian/strptime.rb', line 52

def time_strptime(string, format, now = Time.now)
  Time.strptime(normalize_input(string, format), format, now)
end