Debian Codegraph @

lib/tomlrb/local_time.rb

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