Class: Circa::Time
Constant Summary collapse
- REGEX =
/^([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
A new instance of Time.
- #to_s ⇒ Object
- #to_time ⇒ Object
- #valid_parts ⇒ Object
Methods included from Util
Constructor Details
#initialize(time_string) ⇒ Time
Returns a new instance of Time.
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/circa/time.rb', line 11 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 ⇒ Object
23 24 25 |
# File 'lib/circa/time.rb', line 23 def to_s "#{@date.to_s} #{@hour}:#{@minute}:#{@second}" end |
#to_time ⇒ Object
32 33 34 35 36 |
# File 'lib/circa/time.rb', line 32 def to_time parts = [:year, :month, :day, :hour, :minute, :second] args = valid_parts_as_args(parts) ::DateTime.send(:new, *args) end |
#valid_parts ⇒ Object
27 28 29 30 |
# File 'lib/circa/time.rb', line 27 def valid_parts time_parts = { hour: @hour, minute: @minute, second: @second } @date.valid_parts.merge(time_parts) end |