Class: PerfectTOML::LocalDate

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

Overview

Represents TOML's Local Date

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

Constant Summary collapse

DAYS_IN_MONTH =
[31, nil, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]

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.

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
# File 'lib/perfect_toml.rb', line 106

def initialize(year, month, day)
  @year = year.to_i
  @month = month.to_i
  raise ArgumentError, "month must be in 1..12" unless 1 <= @month && @month <= 12
  @day = day.to_i
  max_day = DAYS_IN_MONTH[@month - 1] || (@year % 4 == 0 && (@year % 100 != 0 || @year % 400 == 0) ? 29 : 28)
  raise ArgumentError, "day must be in 1..#{ max_day }" unless 1 <= @day && @day <= max_day
end

Instance Attribute Details

#dayObject (readonly)

Returns the value of attribute day.



115
116
117
# File 'lib/perfect_toml.rb', line 115

def day
  @day
end

#monthObject (readonly)

Returns the value of attribute month.



115
116
117
# File 'lib/perfect_toml.rb', line 115

def month
  @month
end

#yearObject (readonly)

Returns the value of attribute year.



115
116
117
# File 'lib/perfect_toml.rb', line 115

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


135
136
137
# File 'lib/perfect_toml.rb', line 135

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


127
128
129
# File 'lib/perfect_toml.rb', line 127

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