class Circa::Time

Manage partial times.

Constants

REGEX

Match times in format %H:%M:%S

Public Class Methods

new(time_string) click to toggle source

Create a new {Circa::Time} @param [String] time_string

A string in format %Y-%m-%d %H:%M:%S

@raise [ArgumentError]

If an invalid string is given
# File lib/circa/time.rb, line 19
def initialize(time_string)
  parts = time_string.split(/T|\s/)
  @date = Date.new(parts[0])
  @hour = '00'
  @minute = '00'
  @second = '00'
  @valid_parts = {}
  unless validate(parts[1])
    raise ArgumentError, "Invalid time: #{time_string}"
  end
end

Public Instance Methods

to_s() click to toggle source

Get the time as a string @return [String]

{Circa::Date#to_s} plus the time in format %H:%M:%S
# File lib/circa/time.rb, line 34
def to_s
  "#{@date.to_s} #{@hour}:#{@minute}:#{@second}"
end
to_time() click to toggle source

Get the time as a {::DateTime} @return [DateTime]

A {::DateTime}
# File lib/circa/time.rb, line 51
def to_time
  parts = [:year, :month, :day, :hour, :minute, :second]
  args = valid_parts_as_args(parts)
  ::DateTime.send(:new, *args)
end
valid_parts() click to toggle source

Get the valid parts of the time @return [Hash]

A hash with keys [:year, :month, :day, :hour, :minute, :second]
where each of those keys is valid in the time,
and values per the keys
# File lib/circa/time.rb, line 43
def valid_parts
  time_parts = { hour: @hour, minute: @minute, second: @second }
  @date.valid_parts.merge(time_parts)
end

Private Instance Methods

set_time(matches) click to toggle source
# File lib/circa/time.rb, line 65
def set_time(matches)
  @hour, @minute, @second = matches[1..3]
  [@hour, @minute, @second].join.to_i == 0 || @date.valid_parts[:day]
end
validate(time_string) click to toggle source
# File lib/circa/time.rb, line 59
def validate(time_string)
  time_string.chomp!('Z')
  matches = REGEX.match(time_string)
  set_time(matches) if matches
end