Class: Circa::Time
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
-
#initialize(time_string) ⇒ Time
constructor
Create a new Time.
-
#to_s ⇒ String
Get the time as a string.
-
#to_time ⇒ DateTime
Get the time as a DateTime.
-
#valid_parts ⇒ Hash
Get the valid parts of the time.
Constructor Details
#initialize(time_string) ⇒ Time
Create a new Circa::Time
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_s ⇒ String
Get the time as a string
34 35 36 |
# File 'lib/circa/time.rb', line 34 def to_s "#{@date.to_s} #{@hour}:#{@minute}:#{@second}" end |
#to_time ⇒ DateTime
Get the time as 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_parts ⇒ Hash
Get the valid parts of the time
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 |