Class: FLV::Timestamp

Inherits:
Float
  • Object
show all
Defined in:
lib/flvedit/flv/timestamp.rb

Constant Summary collapse

REGEXP =
/^(\d*[h:])?(\d*[m:])?(\d*)\.?(\d*)$/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ms = 0) ⇒ Timestamp

Returns a new instance of Timestamp.

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/flvedit/flv/timestamp.rb', line 6

def initialize(ms=0)
  raise ArgumentError unless ms.is_a? Numeric
  super
end

Class Method Details

.in_milliseconds(ms) ⇒ Object



42
43
44
# File 'lib/flvedit/flv/timestamp.rb', line 42

def self.in_milliseconds(ms)
  Timestamp.new(ms/1000.0)
end

.in_seconds(s) ⇒ Object



46
47
48
# File 'lib/flvedit/flv/timestamp.rb', line 46

def self.in_seconds(s)
  new s
end

.try_convert(s, if_empty_string = 0) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/flvedit/flv/timestamp.rb', line 50

def self.try_convert(s, if_empty_string = 0)
  case s
  when Timestamp
    s
  when Numeric
    new s
  when ""
    new if_empty_string
  when REGEXP
    h, m, s, ms = Regexp.last_match.captures
    ms &&= ms.ljust(3,'0')[0,3] # e.g. 1.23 => 1.230
    h, m = m, h if h and h.end_with?(":") and m.nil?
    h, m, s, ms = [h, m, s, ms].map{|n| (n || 0).to_i}
    in_seconds ((h*60+m)*60+s)+ms/1000.0
  end
end

Instance Method Details

#in_millisecondsObject



34
35
36
# File 'lib/flvedit/flv/timestamp.rb', line 34

def in_milliseconds
  (self * 1000).round
end

#in_secondsObject



30
31
32
# File 'lib/flvedit/flv/timestamp.rb', line 30

def in_seconds
  to_f
end

#to_aObject

Returns [hours, minutes, seconds, milliseconds]



12
13
14
15
16
17
18
19
# File 'lib/flvedit/flv/timestamp.rb', line 12

def to_a
  n = in_milliseconds
  [1000,60,60].map do |div|
    val = n % div
    n /= div
    val
  end.reverse.unshift(n)
end

#to_sObject

Returns “HH:MM:SS.MMMM” like “1:23:45.678” or “1:00.000” for 1 minute.



22
23
24
25
26
27
28
# File 'lib/flvedit/flv/timestamp.rb', line 22

def to_s
  return "" if self == INFINITY
  nbs = to_a.drop_while(&:zero?)
  ms = nbs.pop || 0
  first = nbs.shift || 0
  sprintf("%d%s.%03d", first, nbs.map{|n| sprintf(":%02d",n)}.join, ms)
end

#widen(amount) ⇒ Object



38
39
40
# File 'lib/flvedit/flv/timestamp.rb', line 38

def widen(amount)
  TimestampRange.new(self,self).widen(amount)
end