Class: PerfectTOML::LocalTime

Inherits:
LocalDateTimeBase show all
Defined in:
lib/perfect_toml.rb

Overview

Represents TOML's Local Time

See https://toml.io/en/v1.0.0#local-time

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LocalDateTimeBase

#<=>, #==, #inspect, #pretty_print, #to_inline_toml

Constructor Details

#initialize(hour, min, sec) ⇒ LocalTime

Returns a new instance of LocalTime.

Raises:

  • (ArgumentError)


144
145
146
147
148
149
150
151
# File 'lib/perfect_toml.rb', line 144

def initialize(hour, min, sec)
  @hour = hour.to_i
  raise ArgumentError, "hour must be in 0..23" unless 0 <= @hour && @hour <= 23
  @min = min.to_i
  raise ArgumentError, "min must be in 0..59" unless 0 <= @min && @min <= 59
  @sec = Numeric === sec ? sec : sec.include?(".") ? Rational(sec) : sec.to_i
  raise ArgumentError, "sec must be in 0..60" unless 0 <= @sec && @sec <= 60
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



153
154
155
# File 'lib/perfect_toml.rb', line 153

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



153
154
155
# File 'lib/perfect_toml.rb', line 153

def min
  @min
end

#secObject (readonly)

Returns the value of attribute sec.



153
154
155
# File 'lib/perfect_toml.rb', line 153

def sec
  @sec
end

Instance Method Details

#to_sObject

Returns a string representation in RFC 3339 format

lt = PerfectTOML::LocalTime.new(2, 3, 4)
lt.to_s #=> 02:03:04


173
174
175
176
177
# File 'lib/perfect_toml.rb', line 173

def to_s
  s = "%02d:%02d:%02d" % [@hour, @min, @sec]
  s << ("%11.9f" % (@sec - @sec.floor))[1..] unless Integer === @sec
  s
end

#to_time(year, month, day, zone = nil) ⇒ Object

Converts to a Time object with the local timezone. You need to specify year, month, and day.

ld = PerfectTOML::LocalTime.new(2, 3, 4)
ld.to_time(1970, 1, 1) #=> 1970-01-01 02:03:04 +0900

You can specify timezone by passing the fourth argument.

ld.to_time(1970, 1, 1, "UTC")    #=> 1970-01-01 02:03:04 UTC
ld.to_time(1970, 1, 1, "+00:00") #=> 1970-01-01 02:03:04 +0000


165
166
167
# File 'lib/perfect_toml.rb', line 165

def to_time(year, month, day, zone = nil)
  Time.new(year, month, day, @hour, @min, @sec, zone)
end