Class: PerfectTOML::LocalTime
- Inherits:
-
LocalDateTimeBase
- Object
- LocalDateTimeBase
- PerfectTOML::LocalTime
- Defined in:
- lib/perfect_toml.rb
Overview
Represents TOML's Local Time
Instance Attribute Summary collapse
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#sec ⇒ Object
readonly
Returns the value of attribute sec.
Instance Method Summary collapse
-
#initialize(hour, min, sec) ⇒ LocalTime
constructor
A new instance of LocalTime.
-
#to_s ⇒ Object
Returns a string representation in RFC 3339 format.
-
#to_time(year, month, day, zone = nil) ⇒ Object
Converts to a Time object with the local timezone.
Methods inherited from LocalDateTimeBase
#<=>, #==, #inspect, #pretty_print, #to_inline_toml
Constructor Details
#initialize(hour, min, sec) ⇒ LocalTime
Returns a new instance of LocalTime.
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
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
153 154 155 |
# File 'lib/perfect_toml.rb', line 153 def hour @hour end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
153 154 155 |
# File 'lib/perfect_toml.rb', line 153 def min @min end |
#sec ⇒ Object (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_s ⇒ Object
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 |