Debian Codegraph @

lib/tomlrb/local_date.rb

raw
1 # frozen-string-literal: true
2
3 require 'forwardable'
4
5 module Tomlrb
6 class LocalDate
7 extend Forwardable
8
9 def_delegators :@time, :year, :month, :day
10
11 def initialize(year, month, day)
12 @time = Time.utc(year, month, day, 0, 0, 0)
13 raise ArgumentError, "Invalid Local Date: #{year}-#{month}-#{day}" unless day.to_i == @time.day && month.to_i == @time.month && year.to_i == @time.year
14 end
15
16 # @param offset see {LocalDateTime#to_time}
17 # @return [Time] 00:00:00 of the date
18 def to_time(offset = '-00:00')
19 return @time if offset == '-00:00'
20 Time.new(year, month, day, 0, 0, 0, offset)
21 end
22
23 def to_s
24 @time.strftime('%F')
25 end
26
27 def ==(other)
28 other.is_a?(self.class) &&
29 to_time == other.to_time
30 end
31
32 def inspect
33 "#<#{self.class}: #{self}>"
34 end
35 end
36 end