Class: MicroMicro::Parsers::DateTimeParser

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • string (String, #to_s)

See Also:



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_dateString?

Returns:

  • (String, nil)


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_dateString?

Returns:

  • (String, nil)


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_hoursString?

Returns:

  • (String, nil)


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_minutesString

Returns:

  • (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_dateString?

Returns:

  • (String, nil)


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_timeString?

Returns:

  • (String, nil)


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_timezoneString?

Returns:

  • (String, nil)


83
84
85
# File 'lib/micro_micro/parsers/date_time_parser.rb', line 83

def normalized_timezone
  @normalized_timezone ||= zulu || offset&.tr(":", "")
end

#valueString?

Returns:

  • (String, nil)


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

#valuesHash{Symbol => String, nil}

Returns:

  • (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