Class: Timing::ZoneOffset

Inherits:
TransparentProxy
  • Object
show all
Defined in:
lib/timing/zone_offset.rb

Constant Summary collapse

REGEXP =
/^([+-]?)(\d\d):?(\d\d)$/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds) ⇒ ZoneOffset

Returns a new instance of ZoneOffset.

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/timing/zone_offset.rb', line 6

def initialize(seconds)
  raise ArgumentError, "#{seconds} is not a number" unless seconds.is_a? Numeric
  super
end

Class Method Details

.parse(expression) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/timing/zone_offset.rb', line 23

def self.parse(expression)
  match = REGEXP.match expression.strip

  raise ArgumentError, "Invalid time zone offset #{expression}" unless match

  sign = match.captures[0] == '-' ? -1 : 1
  hours = match.captures[1].to_i
  minutes = match.captures[2].to_i

  new (Interval.hours(hours) + Interval.minutes(minutes)) * sign
end

Instance Method Details

#inspectObject



19
20
21
# File 'lib/timing/zone_offset.rb', line 19

def inspect
  "#{to_s} (#{to_f})"
end

#iso8601Object



15
16
17
# File 'lib/timing/zone_offset.rb', line 15

def iso8601
  "#{sign}#{hour.to_s.rjust(2, '0')}:#{minute.to_s.rjust(2, '0')}"
end

#to_s(separator = nil) ⇒ Object



11
12
13
# File 'lib/timing/zone_offset.rb', line 11

def to_s(separator=nil)
  "#{sign}#{hour.to_s.rjust(2, '0')}#{separator}#{minute.to_s.rjust(2, '0')}"
end