Class: PerfectTOML::LocalDateTime
- Inherits:
-
LocalDateTimeBase
- Object
- LocalDateTimeBase
- PerfectTOML::LocalDateTime
- Defined in:
- lib/perfect_toml.rb
Overview
Represents TOML’s Local Date-Time
Instance Attribute Summary collapse
-
#day ⇒ Object
readonly
Returns the value of attribute day.
-
#hour ⇒ Object
readonly
Returns the value of attribute hour.
-
#min ⇒ Object
readonly
Returns the value of attribute min.
-
#month ⇒ Object
readonly
Returns the value of attribute month.
-
#sec ⇒ Object
readonly
Returns the value of attribute sec.
-
#year ⇒ Object
readonly
Returns the value of attribute year.
Instance Method Summary collapse
-
#initialize(year, month, day, hour, min, sec) ⇒ LocalDateTime
constructor
A new instance of LocalDateTime.
-
#to_s ⇒ Object
Returns a string representation in RFC 3339 format.
-
#to_time(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(year, month, day, hour, min, sec) ⇒ LocalDateTime
Returns a new instance of LocalDateTime.
65 66 67 68 69 70 71 72 |
# File 'lib/perfect_toml.rb', line 65 def initialize(year, month, day, hour, min, sec) @year = year.to_i @month = month.to_i @day = day.to_i @hour = hour.to_i @min = min.to_i @sec = Numeric === sec ? sec : sec.include?(".") ? Rational(sec) : sec.to_i end |
Instance Attribute Details
#day ⇒ Object (readonly)
Returns the value of attribute day.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def day @day end |
#hour ⇒ Object (readonly)
Returns the value of attribute hour.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def hour @hour end |
#min ⇒ Object (readonly)
Returns the value of attribute min.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def min @min end |
#month ⇒ Object (readonly)
Returns the value of attribute month.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def month @month end |
#sec ⇒ Object (readonly)
Returns the value of attribute sec.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def sec @sec end |
#year ⇒ Object (readonly)
Returns the value of attribute year.
74 75 76 |
# File 'lib/perfect_toml.rb', line 74 def year @year end |
Instance Method Details
#to_s ⇒ Object
Returns a string representation in RFC 3339 format
ldt = PerfectTOML::LocalDateTime.new(1970, 1, 1, 2, 3, 4)
ldt.to_s #=> 1970-01-01T02:03:04
93 94 95 96 97 |
# File 'lib/perfect_toml.rb', line 93 def to_s s = "%04d-%02d-%02dT%02d:%02d:%02d" % [@year, @month, @day, @hour, @min, @sec] s << ("%11.9f" % (@sec - @sec.floor))[1..] unless Integer === @sec s end |
#to_time(zone = nil) ⇒ Object
Converts to a Time object with the local timezone.
ldt = PerfectTOML::LocalDateTime.new(1970, 1, 1, 2, 3, 4)
ldt.to_time #=> 1970-01-01 02:03:04 +0900
You can specify timezone by passing an argument.
ldt.to_time("UTC") #=> 1970-01-01 02:03:04 UTC
ldt.to_time("+00:00") #=> 1970-01-01 02:03:04 +0000
85 86 87 |
# File 'lib/perfect_toml.rb', line 85 def to_time(zone = nil) @time = Time.new(@year, @month, @day, @hour, @min, @sec, zone) end |