class Timedelta

Constants

DAY_IN_SECONDS
HOUR_IN_SECONDS

note: there's NO month (for now)!!!

why?  month might be 28,29,30,31  days
 use days e.g. 30.days or 31.days etc.
WEEK_IN_SECONDS
YEAR_IN_SECONDS

note: for year use 365 days for now and NOT 365.25 (1/4)

- why? why not? discuss

Attributes

seconds[R]
to_i[R]
to_int[R]

Public Class Methods

days( days ) click to toggle source
# File lib/units-time/timedelta.rb, line 24
def self.days( days )       new( days * DAY_IN_SECONDS );  end
hours( hours ) click to toggle source
# File lib/units-time/timedelta.rb, line 23
def self.hours( hours )     new( hours * HOUR_IN_SECONDS ); end
minutes( minutes ) click to toggle source
# File lib/units-time/timedelta.rb, line 22
def self.minutes( minutes ) new( minutes * 60 ); end
new( seconds=0 ) click to toggle source
# File lib/units-time/timedelta.rb, line 31
def initialize( seconds=0 )
  @seconds = seconds
end
seconds( seconds ) click to toggle source
# File lib/units-time/timedelta.rb, line 21
def self.seconds( seconds ) new( seconds ); end
weeks( weeks ) click to toggle source
# File lib/units-time/timedelta.rb, line 25
def self.weeks( weeks )     new( weeks * WEEK_IN_SECONDS ); end
years( years ) click to toggle source
# File lib/units-time/timedelta.rb, line 26
def self.years( years )     new( years * YEAR_IN_SECONDS ); end
zero() click to toggle source

todo/fix: always freeze by default (timedelta is immutable) - why? why not?

# File lib/units-time/timedelta.rb, line 110
def self.zero()  @@zero ||= new(0).freeze; end

Public Instance Methods

*( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 45
def *( other )
  if other.is_a?( Integer )
    self.class.new( @seconds * other )
  else
    raise TypeError.new( "[Timedelta] mul(tiplication) - wrong type >#{other.inspect}< #{other.class.name} - integer number expected" )
  end
end
+( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 53
def +( other )
  if other.is_a?( self.class )
    self.class.new( @seconds + other.seconds )
  else
    raise TypeError.new( "[Timedelta] add(ition) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end
-( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 61
def -( other )
  if other.is_a?( self.class )
    self.class.new( @seconds - other.seconds )
  else
    raise TypeError.new( "[Timedelta] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end
<=>( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 98
def <=>( other )
  if other.is_a?( self.class )
    @seconds <=> other.seconds
  else
    raise TypeError.new( "[Timedelta] <=> - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end
==( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 36
def ==( other )
  if other.is_a?( self.class )
    @seconds == other.seconds
  else
    false
  end
end
Also aliased as: eql?
coerce( other ) click to toggle source
# File lib/units-time/timedelta.rb, line 87
def coerce( other )
  if other.is_a?( Integer )
    [SafeInteger.new(other), self]
  else
    raise TypeError.new( "[Timedelta] coerce - wrong type >#{other.inspect}< #{other.class.name} - Integer number expected" )
  end
end
eql?( other )
Alias for: ==
zero?() click to toggle source
# File lib/units-time/timedelta.rb, line 107
def zero?() self == self.class.zero; end