class Duration

Public Class Methods

new(seconds) click to toggle source
# File lib/duration.rb, line 7
def initialize seconds
  raise NoTimeError.new "Time must be set." if seconds.nil?

  @seconds = seconds.to_f
  raise NegativeTimeError.new "Duration cannot be negative." if @seconds < 0
end

Public Instance Methods

+(add) click to toggle source
# File lib/duration.rb, line 18
def + add
  shifted = @seconds + add
  Duration.new(shifted)
end
-(subtract) click to toggle source
# File lib/duration.rb, line 23
def - subtract
  self + (-subtract)
end
<=>(compare) click to toggle source
# File lib/duration.rb, line 27
def <=> compare
  to_f <=> compare.to_f
end
to_f() click to toggle source
# File lib/duration.rb, line 14
def to_f
  @seconds
end
to_s() click to toggle source
# File lib/duration.rb, line 31
def to_s
  seconds = @seconds % 60
  total_minutes = (@seconds / 60).floor
  minutes = total_minutes % 60
  hours = (total_minutes / 60).floor
  "#{'%02d' % hours}:#{'%02d' % minutes}:#{('%06.3f' % seconds).tr '.', ','}"
end