Class: Timedelta

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/units-time/timedelta.rb

Defined Under Namespace

Classes: SafeInteger

Constant Summary collapse

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.
60 * 60
DAY_IN_SECONDS =

60 minutes * 60 seconds

24 * HOUR_IN_SECONDS
WEEK_IN_SECONDS =

24 hours * 60 * 60

7 * DAY_IN_SECONDS
YEAR_IN_SECONDS =

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

- why? why not? discuss
365 * DAY_IN_SECONDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds = 0) ⇒ Timedelta

Returns a new instance of Timedelta.



31
32
33
# File 'lib/units-time/timedelta.rb', line 31

def initialize( seconds=0 )
  @seconds = seconds
end

Instance Attribute Details

#secondsObject (readonly) Also known as: to_i, to_int

Returns the value of attribute seconds.



29
30
31
# File 'lib/units-time/timedelta.rb', line 29

def seconds
  @seconds
end

Class Method Details

.days(days) ⇒ Object



24
# File 'lib/units-time/timedelta.rb', line 24

def self.days( days )       new( days * DAY_IN_SECONDS );  end

.hours(hours) ⇒ Object



23
# File 'lib/units-time/timedelta.rb', line 23

def self.hours( hours )     new( hours * HOUR_IN_SECONDS ); end

.minutes(minutes) ⇒ Object



22
# File 'lib/units-time/timedelta.rb', line 22

def self.minutes( minutes ) new( minutes * 60 ); end

.seconds(seconds) ⇒ Object

365 days * 24 * 60 * 60



21
# File 'lib/units-time/timedelta.rb', line 21

def self.seconds( seconds ) new( seconds ); end

.weeks(weeks) ⇒ Object



25
# File 'lib/units-time/timedelta.rb', line 25

def self.weeks( weeks )     new( weeks * WEEK_IN_SECONDS ); end

.years(years) ⇒ Object



26
# File 'lib/units-time/timedelta.rb', line 26

def self.years( years )     new( years * YEAR_IN_SECONDS ); end

.zeroObject

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



110
# File 'lib/units-time/timedelta.rb', line 110

def self.zero()  @@zero ||= new(0).freeze; end

Instance Method Details

#*(other) ⇒ Object



45
46
47
48
49
50
51
# 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) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
# 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) ⇒ Object



98
99
100
101
102
103
104
# 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) ⇒ Object Also known as: eql?



36
37
38
39
40
41
42
# File 'lib/units-time/timedelta.rb', line 36

def ==( other )
  if other.is_a?( self.class )
    @seconds == other.seconds
  else
    false
  end
end

#coerce(other) ⇒ Object



87
88
89
90
91
92
93
# 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

#zero?Boolean

note: compares values (e.g. 0==0) - not object_id (or frozen) etc.

Returns:

  • (Boolean)


107
# File 'lib/units-time/timedelta.rb', line 107

def zero?() self == self.class.zero; end