File: local_date.rb

package info (click to toggle)
ruby-tomlrb 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 136 kB
  • sloc: ruby: 1,090; yacc: 229; makefile: 4
file content (36 lines) | stat: -rw-r--r-- 847 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen-string-literal: true

require 'forwardable'

module Tomlrb
  class LocalDate
    extend Forwardable

    def_delegators :@time, :year, :month, :day

    def initialize(year, month, day)
      @time = Time.utc(year, month, day, 0, 0, 0)
      raise ArgumentError, "Invalid Local Date: #{year}-#{month}-#{day}" unless day.to_i == @time.day && month.to_i == @time.month && year.to_i == @time.year
    end

    # @param offset see {LocalDateTime#to_time}
    # @return [Time] 00:00:00 of the date
    def to_time(offset = '-00:00')
      return @time if offset == '-00:00'
      Time.new(year, month, day, 0, 0, 0, offset)
    end

    def to_s
      @time.strftime('%F')
    end

    def ==(other)
      other.is_a?(self.class) &&
        to_time == other.to_time
    end

    def inspect
      "#<#{self.class}: #{self}>"
    end
  end
end