The class is used in Momomoto to represent time intervals.

Methods
Included Modules
Classes and Modules
Class TimeInterval::ParseError
Attributes
[R] hour Getter methods for hours, minutes and seconds
  interval = TimeInterval.new( {:hour => 42, :min => 23} )
    time.hour => 42
    time.min => 23
    time.sec => 0
[R] min Getter methods for hours, minutes and seconds
  interval = TimeInterval.new( {:hour => 42, :min => 23} )
    time.hour => 42
    time.min => 23
    time.sec => 0
[R] sec Getter methods for hours, minutes and seconds
  interval = TimeInterval.new( {:hour => 42, :min => 23} )
    time.hour => 42
    time.min => 23
    time.sec => 0
Public Class methods
new( d = {} )

Creates a new instance of TimeInterval with d representing either a value of type Hash

  TimeInterval.new( {:hour => 5}),

a value of type Integer in seconds

  TimeInterval.new( 23 ),

or of type String

  TimeInterval.new( "00:23" ).

Use getter methods hour, min and sec in your code.

# File lib/timeinterval.rb, line 119
  def initialize( d = {} )
    case d
      when Hash then
        init_from_hash( d )
      when Integer then
        init_from_int( d )
      when String then
        parsed = Date._parse( d, false)
        if ( parsed.empty? && d.length > 0 ) || !(parsed.keys - [:hour,:min,:sec,:sec_fraction]).empty?
          raise ParseError, "Could not parse interval `#{d}`"
        end
        init_from_hash( parsed )
    end

  end
parse( interval )

Creates and returns a new instance of TimeInterval from the given interval

  interval = TimeInterval.new( "00:23" )
    => #<TimeInterval:0x5235d458 @hour=0, @sec=0, @min=23>
# File lib/timeinterval.rb, line 31
    def parse( interval )
      TimeInterval.new( interval )
    end
Public Instance methods
+( other )

add to another TimeInterval instance

# File lib/timeinterval.rb, line 53
  def +( other )
    self.class.new( self.to_i + other.to_i )
  end
-( other )

subtract something to a TimeInterval instance

# File lib/timeinterval.rb, line 58
  def -( other )
    self.class.new( self.to_i - other.to_i )
  end
<=>( other )

Compares two TimeInterval instances by converting into seconds and applying #<=> to them. Returns 1 (first > last), 0 (first == last) or -1 (first < last).

  i1 = TimeInterval.new( {:hour => 42, :min => 23, :sec => 2} )
    i1.to_i => 152582
  i2 = TimeInterval.new( {:hour => 5, :min => 23, :sec => 2} )
    i2.to_i => 19382

  i1 <=> i2     #=> 1
# File lib/timeinterval.rb, line 48
  def <=>( other )
    self.to_i <=> other.to_i
  end
strftime( fmt = "%H:%M:%S" )

Formats time interval according to the directives in the given format string.

  i = TimeInterval.new(2342) #2342 seconds
  i.strftime( "%H" )  => "00"
  i.strftime( "%M" )  => "39"
  i.strftime( "%S" )  => "02"
# File lib/timeinterval.rb, line 69
  def strftime( fmt = "%H:%M:%S" )
    fmt.gsub( /%(.)/ ) do | match |
      case match[1,1]
        when 'H' then sprintf('%02d',@hour)
        when 'M' then sprintf('%02d',@min)
        when 'S' then sprintf('%02d',@sec)
        when '%' then '%'
        else match
      end
    end
  end
to_f()

Returns the value of time interval as number of seconds

  Time.now + TimeInterval.new(2342)
    => Tue Dec 11 21:16:06 +0100 2007
# File lib/timeinterval.rb, line 91
  def to_f
    self.to_i.to_f
  end
to_i()

Returns the value of time interval as number of seconds

This method is also aliased as to_int
# File lib/timeinterval.rb, line 82
  def to_i
    @hour * 3600 + @min * 60 + @sec
  end
to_int()

Alias for to_i

to_s()

Returns a string representing time interval. Equivalent to calling Time#strftime with a format string of ’%H:%M:%S’.

  i.inspect  => "#<TimeInterval:0x517e36b8 @hour=0, @sec=2, @min=39>"
  i.to_s     => "00:39:02"
  i.strftime  => "00:39:02"
# File lib/timeinterval.rb, line 101
  def to_s
    strftime( '%H:%M:%S' )
  end