Class: PerfectTOML::LocalTime

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

Overview

Represents TOML’s Local Time

See 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.



140
141
142
143
144
# File 'lib/perfect_toml.rb', line 140

def initialize(hour, min, sec)
  @hour = hour.to_i
  @min = min.to_i
  @sec = Numeric === sec ? sec : sec.include?(".") ? Rational(sec) : sec.to_i
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



146
147
148
# File 'lib/perfect_toml.rb', line 146

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



146
147
148
# File 'lib/perfect_toml.rb', line 146

def min
  @min
end

#secObject (readonly)

Returns the value of attribute sec.



146
147
148
# File 'lib/perfect_toml.rb', line 146

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


166
167
168
169
170
# File 'lib/perfect_toml.rb', line 166

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


158
159
160
# File 'lib/perfect_toml.rb', line 158

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