File: local_time.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 (41 lines) | stat: -rw-r--r-- 1,027 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
41
# frozen-string-literal: true

require 'forwardable'

module Tomlrb
  class LocalTime
    extend Forwardable

    def_delegators :@time, :hour, :min, :sec, :usec, :nsec

    def initialize(hour, min, sec)
      @time = Time.utc(0, 1, 1, hour, min, sec)
      raise ArgumentError, "Invalid Local Time: #{hour}-#{min}-#{sec}" unless min.to_i == @time.min && hour.to_i == @time.hour
      @sec = sec
    end

    # @param year [Integer]
    # @param month [Integer]
    # @param day [Integer]
    # @param offset see {LocalDateTime#to_time}
    # @return [Time] the time of the date specified by params
    def to_time(year, month, day, offset = '-00:00')
      Time.new(year, month, day, hour, min, @sec, offset)
    end

    def to_s
      frac = (@sec - sec)
      frac_str = frac.zero? ? '' : frac.to_s[1..-1]
      @time.strftime('%T') << frac_str
    end

    def ==(other)
      other.is_a?(self.class) &&
        @time == other.to_time(0, 1, 1)
    end

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