Class: PerfectTOML::LocalDateTime

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

Overview

Represents TOML’s Local Date-Time

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dayObject (readonly)

Returns the value of attribute day.



74
75
76
# File 'lib/perfect_toml.rb', line 74

def day
  @day
end

#hourObject (readonly)

Returns the value of attribute hour.



74
75
76
# File 'lib/perfect_toml.rb', line 74

def hour
  @hour
end

#minObject (readonly)

Returns the value of attribute min.



74
75
76
# File 'lib/perfect_toml.rb', line 74

def min
  @min
end

#monthObject (readonly)

Returns the value of attribute month.



74
75
76
# File 'lib/perfect_toml.rb', line 74

def month
  @month
end

#secObject (readonly)

Returns the value of attribute sec.



74
75
76
# File 'lib/perfect_toml.rb', line 74

def sec
  @sec
end

#yearObject (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_sObject

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