Class: Timestamp

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds = Time.now.to_i) ⇒ Timestamp

Returns a new instance of Timestamp.



10
11
12
# File 'lib/units-time/timestamp.rb', line 10

def initialize( seconds=Time.now.to_i )
  @seconds = seconds    ## seconds since Jan 1st, 1970 (unix epoch time)
end

Instance Attribute Details

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

Returns the value of attribute seconds.



8
9
10
# File 'lib/units-time/timestamp.rb', line 8

def seconds
  @seconds
end

Class Method Details

.nowObject



6
# File 'lib/units-time/timestamp.rb', line 6

def self.now() new( Time.now.to_i ); end

.zeroObject

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



52
# File 'lib/units-time/timestamp.rb', line 52

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

Instance Method Details

#+(other) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/units-time/timestamp.rb', line 23

def +( other )
  if other.is_a?( Timedelta )
    self.class.new( @seconds + other.seconds )
  else
    raise TypeError.new( "[Timestamp] add(ition) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#-(other) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/units-time/timestamp.rb', line 31

def -( other )
  if other.is_a?( Timedelta )
    self.class.new( @seconds - other.seconds )
  else
    raise TypeError.new( "[Timestamp] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#<=>(other) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/units-time/timestamp.rb', line 42

def <=>( other )
  if other.is_a?( self.class )
    @seconds <=> other.seconds
  else
    raise TypeError.new( "[Timestamp] <=> - wrong type >#{other.inspect}< #{other.class.name} - Timestamp expected" )
  end
end

#==(other) ⇒ Object Also known as: eql?



14
15
16
17
18
19
20
# File 'lib/units-time/timestamp.rb', line 14

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