class SubtitleIt::Subtime

A kinda of Time

Attributes

hrs[RW]
min[RW]
ms[RW]
sec[RW]

Public Class Methods

new(data) click to toggle source
# File lib/subtitle_it/subtime.rb, line 8
def initialize(data)
  fail if data.nil?
  parse_data(data)
end

Public Instance Methods

+(other) click to toggle source
# File lib/subtitle_it/subtime.rb, line 41
def +(other)
  Subtime.new(to_i + other.to_i)
end
-(other) click to toggle source
# File lib/subtitle_it/subtime.rb, line 45
def -(other)
  Subtime.new(to_i - other.to_i)
end
<=>(other) click to toggle source
# File lib/subtitle_it/subtime.rb, line 49
def <=>(other)
  to_i <=> other.to_i
end
parse_data(data) click to toggle source

parses string like '00:00:00,000' or single number as ms.

# File lib/subtitle_it/subtime.rb, line 14
def parse_data(data)
  case data
  when Numeric
    @sec, @ms  = data.divmod(1000)
    @min, @sec = @sec.divmod(60)
    @hrs, @min = @min.divmod(60)
  when String
    time, float = data.split(/\.|\,/)
    time = time.split(/:/).map(&:to_i)
    @ms =  (('0.%d' % float.to_i).to_f * 1000).to_i if float
    @sec, @min, @hrs = time.reverse
  else fail 'Format unknown.'
  end
  # FIXME: dunno what to do about this.. nil = problems with to_i
  @hrs ||= 0; @min ||= 0; @sec ||= 0;  @ms ||= 0
end
to_i() click to toggle source

return time as a total in ms

# File lib/subtitle_it/subtime.rb, line 37
def to_i
  (@hrs * 3600 + @min * 60 + @sec) * 1000 + @ms
end
to_s(sep = '.') click to toggle source

to_s(separator) => to_s(“,”) => 00:00:00,000

# File lib/subtitle_it/subtime.rb, line 32
def to_s(sep = '.')
  "%02d:%02d:%02d#{sep}%03d" % [@hrs, @min, @sec,  @ms]
end