Module: Wheneverd::Systemd::TimeParser

Defined in:
lib/wheneverd/systemd/time_parser.rb

Overview

Parses human-friendly times into HH:MM:SS for systemd OnCalendar= specs.

Class Method Summary collapse

Class Method Details

.parse(str) ⇒ String

Returns time in HH:MM:SS format.

Parameters:

  • str (String)

Returns:

  • (String)

    time in HH:MM:SS format

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wheneverd/systemd/time_parser.rb', line 10

def self.parse(str)
  input = str.to_s.strip
  raise InvalidTimeError, "Invalid time: empty" if input.empty?

  if (match = /\A(?<h>\d{1,2}):(?<m>\d{2})(?::(?<s>\d{2}))?\z/.match(input))
    return parse_24h(match)
  end

  if (match = /\A(?<h>\d{1,2})(?::(?<m>\d{2}))?\s*(?<ampm>am|pm)\z/i.match(input))
    return parse_12h(match)
  end

  raise InvalidTimeError, "Invalid time: #{input.inspect}"
end