class Timestamp
Attributes
seconds[R]
to_i[R]
to_int[R]
Public Class Methods
new( seconds=Time.now.to_i )
click to toggle source
# 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
now()
click to toggle source
todo: double check that Time.now.to_i always returns seconds in utc/gmt “universal” time (and not local time)
# File lib/units-time/timestamp.rb, line 7 def self.now() new; end
zero()
click to toggle source
todo/fix: always freeze by default (timestamp is immutable) - why? why not?
# File lib/units-time/timestamp.rb, line 57 def self.zero() @@zero ||= new(0).freeze; end
Public Instance Methods
+( other )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# 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 )
click to toggle source
# File lib/units-time/timestamp.rb, line 15 def ==( other ) if other.is_a?( self.class ) @seconds == other.seconds else false end end
Also aliased as: eql?
zero?()
click to toggle source
# File lib/units-time/timestamp.rb, line 54 def zero?() self == self.class.zero; end