Class: PerfectTOML::LocalDate

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

Overview

Represents TOML’s Local Date

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from LocalDateTimeBase

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

Constructor Details

#initialize(year, month, day) ⇒ LocalDate

Returns a new instance of LocalDate.



104
105
106
107
108
# File 'lib/perfect_toml.rb', line 104

def initialize(year, month, day)
  @year = year.to_i
  @month = month.to_i
  @day = day.to_i
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



110
111
112
# File 'lib/perfect_toml.rb', line 110

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month.



110
111
112
# File 'lib/perfect_toml.rb', line 110

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



110
111
112
# File 'lib/perfect_toml.rb', line 110

def year
  @year
end

Instance Method Details

#to_sObject

Returns a string representation in RFC 3339 format

ld = PerfectTOML::LocalDate.new(1970, 1, 1)
ld.to_s #=> 1970-01-01


130
131
132
# File 'lib/perfect_toml.rb', line 130

def to_s
  "%04d-%02d-%02d" % [@year, @month, @day]
end

#to_time(zone = nil) ⇒ Object

Converts to a Time object with the local timezone. Its time will be 00:00:00.

ld = PerfectTOML::LocalDate.new(1970, 1, 1)
ld.to_time #=> 1970-01-01 00:00:00 +0900

You can specify timezone by passing an argument.

ld.to_time("UTC")    #=> 1970-01-01 00:00:00 UTC
ld.to_time("+00:00") #=> 1970-01-01 00:00:00 +0000


122
123
124
# File 'lib/perfect_toml.rb', line 122

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