Class: SubtitleIt::Subtime

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/subtitle_it/subtime.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Subtime

Returns a new instance of Subtime.



7
8
9
10
# File 'lib/subtitle_it/subtime.rb', line 7

def initialize(data)
  raise if data == nil
  parse_data(data)
end

Instance Attribute Details

#hrsObject

Returns the value of attribute hrs.



5
6
7
# File 'lib/subtitle_it/subtime.rb', line 5

def hrs
  @hrs
end

#minObject

Returns the value of attribute min.



5
6
7
# File 'lib/subtitle_it/subtime.rb', line 5

def min
  @min
end

#msObject

Returns the value of attribute ms.



5
6
7
# File 'lib/subtitle_it/subtime.rb', line 5

def ms
  @ms
end

#secObject

Returns the value of attribute sec.



5
6
7
# File 'lib/subtitle_it/subtime.rb', line 5

def sec
  @sec
end

Instance Method Details

#+(other) ⇒ Object



40
# File 'lib/subtitle_it/subtime.rb', line 40

def +(other);   Subtime.new(self.to_i + other.to_i);    end

#-(other) ⇒ Object



41
# File 'lib/subtitle_it/subtime.rb', line 41

def -(other);   Subtime.new(self.to_i - other.to_i);    end

#<=>(other) ⇒ Object



42
# File 'lib/subtitle_it/subtime.rb', line 42

def <=>(other);      self.to_i <=> other.to_i;    end

#parse_data(data) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/subtitle_it/subtime.rb', line 13

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 { |s| s.to_i }
    @ms =  (("0.%d" % float.to_i).to_f * 1000).to_i if float
    @sec, @min, @hrs = time.reverse
  else raise "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_iObject

return time as a total in ms



36
37
38
# File 'lib/subtitle_it/subtime.rb', line 36

def to_i
  ( @hrs * 3600 + @min * 60 + @sec ) * 1000 + @ms
end

#to_s(sep = '.') ⇒ Object

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



31
32
33
# File 'lib/subtitle_it/subtime.rb', line 31

def to_s(sep='.')
  "%02d:%02d:%02d#{sep}%03d" % [@hrs, @min, @sec,  @ms]
end