Class: Timestamp
- Inherits:
-
Object
- Object
- Timestamp
- Includes:
- Comparable
- Defined in:
- lib/units-time/timestamp.rb
Instance Attribute Summary collapse
-
#seconds ⇒ Object
(also: #to_i, #to_int)
readonly
Returns the value of attribute seconds.
Class Method Summary collapse
-
.now ⇒ Object
todo: double check that Time.now.to_i always returns seconds in utc/gmt “universal” time (and not local time).
-
.zero ⇒ Object
todo/fix: always freeze by default (timestamp is immutable) - why? why not?.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
-
#initialize(seconds = Time.now.to_i) ⇒ Timestamp
constructor
A new instance of Timestamp.
-
#zero? ⇒ Boolean
note: compares values (e.g. 0==0) - not object_id (or frozen) etc.
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
#seconds ⇒ Object (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
.now ⇒ Object
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 |
.zero ⇒ Object
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.
54 |
# File 'lib/units-time/timestamp.rb', line 54 def zero?() self == self.class.zero; end |