Class: Circa::Time

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/circa/time.rb

Overview

Manage partial times.

Constant Summary collapse

REGEX =

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

/^([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])Z?$/

Instance Method Summary collapse

Constructor Details

#initialize(time_string) ⇒ Time

Create a new Circa::Time

Parameters:

  • time_string (String)

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

Raises:

  • (ArgumentError)

    If an invalid string is given



19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#to_sString

Get the time as a string

Returns:

  • (String)

    Date#to_s plus the time in format %H:%M:%S



34
35
36
# File 'lib/circa/time.rb', line 34

def to_s
  "#{@date.to_s} #{@hour}:#{@minute}:#{@second}"
end

#to_timeDateTime

Get the time as a DateTime

Returns:

  • (DateTime)

    A DateTime



51
52
53
54
55
# 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_partsHash

Get the valid parts of the time

Returns:

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



43
44
45
46
# 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