File: local_date_time.rb

package info (click to toggle)
ruby-tomlrb 2.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 132 kB
  • sloc: ruby: 999; yacc: 195; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 1,261 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
37
38
39
40
require 'forwardable'

module Tomlrb
  class LocalDateTime
    extend Forwardable

    def_delegators :@time, :year, :month, :day, :hour, :min, :sec, :usec, :nsec

    def initialize(year, month, day, hour, min, sec) # rubocop:disable Metrics/ParameterLists
      @time = Time.new(year, month, day, hour, min, sec, '-00:00')
      @sec = sec
    end

    # @param offset [String, Symbol, Numeric, nil] time zone offset.
    #   * when +String+, must be '+HH:MM' format, '-HH:MM' format, 'UTC', 'A'..'I' or 'K'..'Z'. Arguments excluding '+-HH:MM' are supporeted at Ruby >= 2.7.0
    #   * when +Symbol+, must be +:dst+(for summar time for local) or +:std+(for standard time).
    #   * when +Numeric+, it is time zone offset in second.
    #   * when +nil+, local time zone offset is used.
    # @return [Time]
    def to_time(offset='-00:00')
      return @time if offset == '-00:00'
      Time.new(year, month, day, hour, min, @sec, offset)
    end

    def to_s
      frac = (@sec - sec)
      frac_str = frac == 0 ? '' : "#{frac.to_s[1..-1]}"
      @time.strftime("%FT%T") << frac_str
    end

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

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