Class: CompositeUnitMeasurements::Time

Inherits:
Object
  • Object
show all
Defined in:
lib/composite_unit_measurements/time.rb

Overview

A parser handling time measurements, particularly for composite units like hour:minute:second,microsecond, minute-second, hour-minute, etc.

Author:

Since:

  • 0.2.0

Constant Summary collapse

HOUR_ALIASES =

Regex pattern for aliases of hour unit.

Author:

Since:

  • 0.3.0

/(?:h|hr|hour(?:s)?)/.freeze
MINUTE_ALIASES =

Regex pattern for aliases of minute unit.

Author:

Since:

  • 0.3.0

/(?:min|minute(?:s)?)/.freeze
SECOND_ALIASES =

Regex pattern for aliases of second unit.

Author:

Since:

  • 0.5.0

/(?:s|sec|second(?:s)?)/.freeze
DAY_ALIASES =

Regex pattern for aliases of day unit.

Author:

Since:

  • 0.5.0

/(?:d|day(?:s)?)/.freeze
WEEK_ALIASES =

Regex pattern for aliases of week unit.

Author:

Since:

  • 0.5.0

/(?:wk|week(?:s)?)/.freeze
MONTH_ALIASES =

Regex pattern for aliases of month unit.

Author:

Since:

  • 0.5.0

/(?:mo|month(?:s)?)/.freeze
HOUR_MINUTE =

Regex pattern for parsing a time measurement in the format of hour-minute.

Author:

Since:

  • 0.3.0

/\A#{ANY_NUMBER}\s*#{HOUR_ALIASES}\s*#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\z/.freeze
DURATION =

Regex pattern for parsing duration in the format of hour:minute:second or hour:minute:second,microsecond.

Author:

Since:

  • 0.2.0

/\A(?<hour>#{REAL_NUMBER}):(?<min>#{REAL_NUMBER}):(?:(?<sec>#{REAL_NUMBER}))?(?:,(?<msec>#{REAL_NUMBER}))?\z/.freeze
MINUTE_SECOND =

Regex pattern for parsing a time measurement in the format of minute-second.

Author:

Since:

  • 0.5.0

/\A#{ANY_NUMBER}\s*#{MINUTE_ALIASES}\s*#{ANY_NUMBER}\s*#{SECOND_ALIASES}\z/.freeze
WEEK_DAY =

Regex pattern for parsing a time measurement in the format of week-day.

Author:

Since:

  • 0.5.0

/\A#{ANY_NUMBER}\s*#{WEEK_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze
MONTH_DAY =

Regex pattern for parsing a time measurement in the format of month-day.

Author:

Since:

  • 0.5.0

/\A#{ANY_NUMBER}\s#{MONTH_ALIASES}\s*#{ANY_NUMBER}\s*#{DAY_ALIASES}\z/.freeze

Class Method Summary collapse

Class Method Details

.parse(string) ⇒ UnitMeasurements::Time

Parses a given string into a UnitMeasurements::Time object.

Examples:

Parse ‘hour-minute’ measurement:

CompositeUnitMeasurements::Time.parse("3 h 45 min") #=> 3.75 h

Parse ‘duration’:

CompositeUnitMeasurements::Time.parse("12:60:3600,360000000") #=> 14.1 h
CompositeUnitMeasurements::Time.parse("12:60:3600") #=> 14.0 h

Parse ‘minute-second’ measurement:

CompositeUnitMeasurements::Time.parse("10 min 90 s") #=> 11.5 min

Parse ‘week-day’ measurement:

CompositeUnitMeasurements::Time.parse("8 wk 3 d") #=> 8.428571428571429 wk

Parse ‘month-day’ measurement:

CompositeUnitMeasurements::Time.parse("2 mo 60 d") #=> 3.97260057797197 mo

Parameters:

  • string (String)

    The string to parse for time measurement.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

Raises:

  • (UnitMeasurements::ParseError)

    If the string does not match any known format.

Author:

Since:

  • 0.2.0



37
38
39
40
41
42
43
44
45
46
# File 'lib/composite_unit_measurements/time.rb', line 37

def parse(string)
  case string
  when HOUR_MINUTE   then parse_hour_minute(string)
  when DURATION      then parse_duration(string)
  when MINUTE_SECOND then parse_minute_second(string)
  when WEEK_DAY      then parse_week_day(string)
  when MONTH_DAY     then parse_month_day(string)
  else                    raise UnitMeasurements::ParseError, string
  end
end

.parse_duration(string) ⇒ UnitMeasurements::Time (private)

Parses a string representing time duration in the format of hour:minute:second,microsecond or hour:minute:second into a UnitMeasurements::Time object.

Parameters:

  • string (String)

    The string representing time duration.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

Raises:

  • (ArgumentError)

    Raises an ArgumentError for an invalid duration format.

See Also:

Author:

Since:

  • 0.2.0



84
85
86
87
88
89
90
91
92
# File 'lib/composite_unit_measurements/time.rb', line 84

def parse_duration(string)
  hour, minute, second, microsecond = string.match(DURATION)&.captures
  raise ArgumentError, "Invalid Duration" if [hour, minute, second, microsecond].all?(&:nil?)

  UnitMeasurements::Time.new((hour || 0), "h") +
    UnitMeasurements::Time.new((minute || 0), "min") +
    UnitMeasurements::Time.new((second || 0), "s") +
    UnitMeasurements::Time.new((microsecond || 0), "μs")
end

.parse_hour_minute(string) ⇒ UnitMeasurements::Time (private)

Parses a string representing a time in the format of hour-minute into a UnitMeasurements::Time object.

Parameters:

  • string (String)

    The string representing time measurement in the format of hour-minute.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

See Also:

Author:

Since:

  • 0.3.0



62
63
64
65
66
67
68
# File 'lib/composite_unit_measurements/time.rb', line 62

def parse_hour_minute(string)
  hour, minute = string.match(HOUR_MINUTE)&.captures

  if hour && minute
    UnitMeasurements::Time.new(hour, "h") + UnitMeasurements::Time.new(minute, "min")
  end
end

.parse_minute_second(string) ⇒ UnitMeasurements::Time (private)

Parses a string representing a time in the format of minute-second into a UnitMeasurements::Time object.

Parameters:

  • string (String)

    The string representing time measurement in the format of minute-second.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

See Also:

Author:

Since:

  • 0.5.0



106
107
108
109
110
111
112
# File 'lib/composite_unit_measurements/time.rb', line 106

def parse_minute_second(string)
  minute, second = string.match(MINUTE_SECOND)&.captures

  if minute && second
    UnitMeasurements::Time.new(minute, "min") + UnitMeasurements::Time.new(second, "s")
  end
end

.parse_month_day(string) ⇒ UnitMeasurements::Time (private)

Parses a string representing a time in the format of month-day into a UnitMeasurements::Time object.

Parameters:

  • string (String)

    The string representing time measurement in the format of month-day.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

See Also:

Author:

Since:

  • 0.5.0



146
147
148
149
150
151
152
# File 'lib/composite_unit_measurements/time.rb', line 146

def parse_month_day(string)
  month, day = string.match(MONTH_DAY)&.captures

  if month && day
    UnitMeasurements::Time.new(month, "mo") + UnitMeasurements::Time.new(day, "d")
  end
end

.parse_week_day(string) ⇒ UnitMeasurements::Time (private)

Parses a string representing a time in the format of week-day into a UnitMeasurements::Time object.

Parameters:

  • string (String)

    The string representing time measurement in the format of week-day.

Returns:

  • (UnitMeasurements::Time)

    Returns a UnitMeasurements::Time object if parsing is successful.

See Also:

Author:

Since:

  • 0.5.0



126
127
128
129
130
131
132
# File 'lib/composite_unit_measurements/time.rb', line 126

def parse_week_day(string)
  week, day = string.match(WEEK_DAY)&.captures

  if week && day
    UnitMeasurements::Time.new(week, "wk") + UnitMeasurements::Time.new(day, "d")
  end
end