Sha256: a7959a62b24991826f16ad69f450062658fa0a1b8c0b154f1364f4dc59a26876

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 KB

Contents

module Timing
  class ZoneOffset < TransparentProxy

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

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

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

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

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

    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

    private

    def hour
      Interval.new(to_f).to_hours.to_i
    end

    def minute
      Interval.new(to_f % Interval.hours(1)).to_minutes.to_i
    end

    def sign
      self < 0 ? '-' : '+'
    end

  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
timing-0.2.7 lib/timing/zone_offset.rb
timing-0.2.6 lib/timing/zone_offset.rb
timing-0.2.5 lib/timing/zone_offset.rb
timing-0.2.4 lib/timing/zone_offset.rb
timing-0.2.3 lib/timing/zone_offset.rb
timing-0.2.2 lib/timing/zone_offset.rb
timing-0.2.0 lib/timing/zone_offset.rb
timing-0.1.3 lib/timing/zone_offset.rb
timing-0.1.2 lib/timing/zone_offset.rb
timing-0.1.1 lib/timing/zone_offset.rb
timing-0.1.0 lib/timing/zone_offset.rb
timing-0.0.11 lib/timing/zone_offset.rb
timing-0.0.10 lib/timing/zone_offset.rb
timing-0.0.9 lib/timing/zone_offset.rb
timing-0.0.8 lib/timing/zone_offset.rb
timing-0.0.7 lib/timing/zone_offset.rb
timing-0.0.6 lib/timing/zone_offset.rb