Class: TimeInterval

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

Overview

The class is used in Momomoto to represent time intervals.

Defined Under Namespace

Classes: ParseError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(d = {}) ⇒ TimeInterval

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.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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

Instance Attribute Details

#hourObject (readonly)

Getter methods for hours, minutes and seconds

interval = TimeInterval.new( {:hour => 42, :min => 23} )
  time.hour => 42
  time.min => 23
  time.sec => 0


24
25
26
# File 'lib/timeinterval.rb', line 24

def hour
  @hour
end

#minObject (readonly)

Getter methods for hours, minutes and seconds

interval = TimeInterval.new( {:hour => 42, :min => 23} )
  time.hour => 42
  time.min => 23
  time.sec => 0


24
25
26
# File 'lib/timeinterval.rb', line 24

def min
  @min
end

#secObject (readonly)

Getter methods for hours, minutes and seconds

interval = TimeInterval.new( {:hour => 42, :min => 23} )
  time.hour => 42
  time.min => 23
  time.sec => 0


24
25
26
# File 'lib/timeinterval.rb', line 24

def sec
  @sec
end

Class Method Details

.parse(interval) ⇒ Object

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>


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

def parse( interval )
  TimeInterval.new( interval )
end

Instance Method Details

#+(other) ⇒ Object

add to another TimeInterval instance



53
54
55
# File 'lib/timeinterval.rb', line 53

def +( other )
  self.class.new( self.to_i + other.to_i )
end

#-(other) ⇒ Object

subtract something to a TimeInterval instance



58
59
60
# File 'lib/timeinterval.rb', line 58

def -( other )
  self.class.new( self.to_i - other.to_i )
end

#<=>(other) ⇒ Object

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


48
49
50
# File 'lib/timeinterval.rb', line 48

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

#strftime(fmt = "%H:%M:%S") ⇒ Object

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"


69
70
71
72
73
74
75
76
77
78
79
# 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_fObject

Returns the value of time interval as number of seconds

Time.now + TimeInterval.new(2342)
  => Tue Dec 11 21:16:06 +0100 2007


91
92
93
# File 'lib/timeinterval.rb', line 91

def to_f
  self.to_i.to_f
end

#to_iObject Also known as: to_int

Returns the value of time interval as number of seconds



82
83
84
# File 'lib/timeinterval.rb', line 82

def to_i
  @hour * 3600 + @min * 60 + @sec
end

#to_sObject

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"


101
102
103
# File 'lib/timeinterval.rb', line 101

def to_s
  strftime( '%H:%M:%S' )
end