Class: MicroMicro::Parsers::DateTimeParser
- Inherits:
-
Object
- Object
- MicroMicro::Parsers::DateTimeParser
- Defined in:
- lib/micro_micro/parsers/date_time_parser.rb
Constant Summary collapse
- DATE_REGEXP_PATTERN =
Regexp pattern matching YYYY-MM-DD and YYY-DDD
'(?<year>\d{4})-' \ '((?<ordinal>3[0-6]{2}|[0-2]\d{2})|(?<month>0\d|1[0-2])-' \ '(?<day>3[0-1]|[0-2]\d))'
- TIME_REGEXP_PATTERN =
Regexp pattern matching HH:MM and HH:MM:SS
'(?<hours>2[0-3]|[0-1]?\d)' \ '(?::(?<minutes>[0-5]\d))?' \ '(?::(?<seconds>[0-5]\d))?' \ '(?:\s*?(?<abbreviation>[apPP]\.?[mM]\.?))?'
- TIMEZONE_REGEXP_PATTERN =
Regexp pattern matching +/-(XX:YY|XXYY|XX) or the literal string Z
'(?<zulu>Z)|(?<offset>(?:\+|-)(?:1[0-2]|0?\d)(?::?[0-5]\d)?)'
- DATE_TIME_TIMEZONE_REGEXP =
Regexp for extracting named captures from a datetime-esque String.
/ \A (?=.) (?:#{DATE_REGEXP_PATTERN})? (?:\s?#{TIME_REGEXP_PATTERN}(?:#{TIMEZONE_REGEXP_PATTERN})?)? \z /x
Instance Method Summary collapse
-
#initialize(string) ⇒ DateTimeParser
constructor
Parse a string for date and/or time values according to the Microformats Value Class Pattern date and time parsing specification.
- #normalized_calendar_date ⇒ String?
- #normalized_date ⇒ String?
- #normalized_hours ⇒ String?
- #normalized_minutes ⇒ String
- #normalized_ordinal_date ⇒ String?
- #normalized_time ⇒ String?
- #normalized_timezone ⇒ String?
- #value ⇒ String?
- #values ⇒ Hash{Symbol => String, nil}
Constructor Details
#initialize(string) ⇒ DateTimeParser
Parse a string for date and/or time values according to the Microformats Value Class Pattern date and time parsing specification.
36 37 38 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 36 def initialize(string) @string = string.to_s end |
Instance Method Details
#normalized_calendar_date ⇒ String?
48 49 50 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 48 def normalized_calendar_date @normalized_calendar_date ||= "#{year}-#{month}-#{day}" if year? && month? && day? end |
#normalized_date ⇒ String?
53 54 55 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 53 def normalized_date @normalized_date ||= normalized_calendar_date || normalized_ordinal_date end |
#normalized_hours ⇒ String?
58 59 60 61 62 63 64 65 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 58 def normalized_hours @normalized_hours ||= if hours? return (hours.to_i + 12).to_s if abbreviation&.tr(".", "")&.downcase == "pm" format("%<hours>02d", hours: hours) end end |
#normalized_minutes ⇒ String
68 69 70 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 68 def normalized_minutes @normalized_minutes ||= minutes || "00" end |
#normalized_ordinal_date ⇒ String?
73 74 75 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 73 def normalized_ordinal_date @normalized_ordinal_date ||= "#{year}-#{ordinal}" if year? && ordinal? end |
#normalized_time ⇒ String?
78 79 80 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 78 def normalized_time @normalized_time ||= [normalized_hours, normalized_minutes, seconds].compact.join(":") if normalized_hours end |
#normalized_timezone ⇒ String?
83 84 85 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 83 def normalized_timezone @normalized_timezone ||= zulu || offset&.tr(":", "") end |
#value ⇒ String?
88 89 90 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 88 def value @value ||= "#{normalized_date} #{normalized_time}#{normalized_timezone}".strip.presence end |
#values ⇒ Hash{Symbol => String, nil}
93 94 95 96 97 98 99 100 |
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 93 def values @values ||= if string.match?(DATE_TIME_TIMEZONE_REGEXP) string.match(DATE_TIME_TIMEZONE_REGEXP).named_captures.transform_keys(&:to_sym) else {} end end |