Sha256: 958ed7c068958739ee299428a9d0503faa5e4d1e981131a585a3b0fc15563cde

Contents?: true

Size: 907 Bytes

Versions: 2

Compression:

Stored size: 907 Bytes

Contents

module Subs
  class SubRipTime

    def initialize(hour, minute, second, millisecond)
      @ms = millisecond
      @ms += second * 1000
      @ms += minute * 60 * 1000
      @ms += hour * 60 * 60 * 1000
    end

    ZERO = SubRipTime.new(0, 0, 0, 0).freeze

    def total_ms
      @ms
    end

    def -(amount)
      value = amount.is_a?(SubRipTime) ? amount.total_ms : Integer(amount)
      self.class.new(0, 0, 0, [@ms - value, 0].max)
    end

    def +(amount)
      value = amount.is_a?(SubRipTime) ? amount.total_ms : Integer(amount)
      self.class.new(0, 0, 0, [@ms + value, 0].max)
    end

    def hours
      @ms / (1000 * 60 * 60)
    end

    def minutes
      (@ms / (1000 * 60)) % 60
    end

    def seconds
      (@ms / 1000) % 60
    end

    def milliseconds
      @ms % 1000
    end

    def to_s
      "%02d:%02d:%02d,%03d" % [hours, minutes, seconds, milliseconds]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
subs-1.0.1 lib/subs/sub_rip_time.rb
subs-1.0.0 lib/subs/sub_rip_time.rb