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.



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

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.



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

def seconds
  @seconds
end

Class Method Details

.nowObject

todo: double check that Time.now.to_i always returns seconds in utc/gmt “universal” time (and not local time)



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

def self.now() new; end

.zeroObject

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



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

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

Instance Method Details

#+(other) ⇒ Object



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

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



32
33
34
35
36
37
38
39
40
41
# File 'lib/units-time/timestamp.rb', line 32

def -( other )
  if other.is_a?( Timedelta )
    self.class.new( @seconds - other.seconds )
  elsif other.is_a?( Timestamp )   ## note: returns Timedelta class/object!!!
    ## todo: check how to deal with negative timedelta - allow !? - why? why not?
    Timedelta.new( @seconds - other.seconds )
  else
    raise TypeError.new( "[Timestamp] sub(straction) - wrong type >#{other.inspect}< #{other.class.name} - Timedelta expected" )
  end
end

#<=>(other) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/units-time/timestamp.rb', line 46

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?



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

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

#zero?Boolean

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

Returns:

  • (Boolean)


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

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