Module: Flexitime::Parser

Included in:
Flexitime
Defined in:
lib/flexitime/parser.rb

Constant Summary collapse

ISO8601_DATE_REGEX =

date regex to match year/month/day eg. “2021-10-27” with 4 digit year; 1 or 2 digit month & day; and slash, hyphen or period separator

%r{(?<!\d)(\d{4})[-./](\d{1,2})[-./](\d{1,2})(?!\d)}
LOCAL_DATE_REGEX =

date regex to match day/month/year and month/day/year eg. “27/10/2021” and “10/27/2021” with 1 or 2 digit day & month; 2 or 4 digit year; and slash, hyphen or period separator

%r{(?<!\d)(\d{1,2})[-./](\d{1,2})[-./](\d{4}|\d{2})(?!\d)}
ISO8601_TIME_REGEX =

time regex to match a zulu time “11:14:30.000Z” with 2 digit hour, minute & second; 1 to 6 digit milliseconds; and Z character

%r{(\d{2}):(\d{2}):(\d{2})\.(\d{1,6})(Z{1})$}
HOUR_MINUTE_SECOND_REGEX =

time regex to match hour/min/sec eg. “09:15:30”, “09:15:30 AM” and “09:15:30AM” with 1 or 2 digit hour; 2 digit minute & second; optional case-insensitive meridiem; and colon separator but not a period separator as there are no rails-i18n locales using that separator with hours, minutes & seconds and it would clash with the local date pattern

%r{^(\d{1,2}):(\d{2}):(\d{2})\s?([aApP][mM])?\.?$}
HOUR_MINUTE_REGEX =

time regex to match hour/min eg. “09:15”, “09:15 AM” and “09:15AM” with 1 or 2 digit hour; 2 digit minute; optional case-insensitive meridiem; and colon or period separator as in rails-i18n there a few locales (such as Danish) that use a period with hours & minutes

%r{^(\d{1,2})[:.](\d{2})\s?([aApP][mM])?\.?$}

Instance Method Summary collapse

Instance Method Details

#parse(str, first_date_part: nil, precision: nil) ⇒ Object

Parse a String argument and create an ActiveSupport::TimeWithZone object using Time.zone

The parse uses either the argument or configuration first_date_part (:day or :month) and either the argument or configuration precision (:day, :hour, :min, :sec or :usec).

When the String contains a date or date/time or time that matches the regular expressions the Time.zone local method is used to create a TimeWithZone object otherwise the Time.zone parse method is used to create a TimeWithZone object and for an invalid date, date/time or time nil is returned.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flexitime/parser.rb', line 34

def parse(str, first_date_part: nil, precision: nil)
  validate_options(first_date_part: first_date_part, precision: precision)

  str = str.is_a?(String) ? str : str.try(:to_str)
  return nil if str.blank?

  parts = extract_parts(str, first_date_part: first_date_part)

  if parts.present?
    create_time_from_parts(parts, precision: precision) if valid_date_parts?(parts)
  else
    time_zone_parse(str, precision: precision)
  end
end