Sha256: ba1cbb344d96872e76069a2cffda235c79da8f079fecaa470e0efea2530b39f0

Contents?: true

Size: 653 Bytes

Versions: 1

Compression:

Stored size: 653 Bytes

Contents

require 'singleton'

module Subshift
  class Time
    include Singleton

    FORMAT = /\d{2,}:\d{2}:\d{2},\d{3}/

    attr_accessor :total_ms

    def self.parse(string)
      h, m, s, ms = string.split(/:|,/).map(&:to_i)

      instance.total_ms = \
        h * 60 * 60 * 1000 +
        m * 60 * 1000 +
        s * 1000 +
        ms

      instance
    end

    def +(seconds)
      tap do |t|
        t.total_ms += seconds * 1000
      end
    end

  private

    def to_s
      h, ms = total_ms.divmod(60 * 60 * 1000)
      m, ms = ms.divmod(60 * 1000)
      s, ms = ms.divmod(1000)

      format '%02d:%02d:%02d,%03d', h, m, s, ms
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
subshift-0.0.2 lib/subshift/time.rb