Class: Agent99::Timestamp

Inherits:
Object
  • Object
show all
Defined in:
lib/agent99/timestamp.rb

Overview

lib/agent99/timestamp.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(now = Time.now) ⇒ Timestamp

regardless of the timezone of “now” timestamps (@ts) is maintained in UTC microseconds



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/agent99/timestamp.rb', line 10

def initialize(now=Time.now)
  if now.is_a? Time
    @ts   = self.class.utc2ts(now.utc)
    @utc  = now.utc
  elsif now.is_a? Integer
    @utc  = self.class.ts2utc(now)
    @ts   = now
  else
    raise ArgumentError, "Expected ether a Time or Integer"
  end
end

Instance Attribute Details

#tsObject (readonly)

Returns the value of attribute ts.



4
5
6
# File 'lib/agent99/timestamp.rb', line 4

def ts
  @ts
end

#utcObject (readonly)

Returns the value of attribute utc.



4
5
6
# File 'lib/agent99/timestamp.rb', line 4

def utc
  @utc
end

Class Method Details

.ts2utc(microseconds) ⇒ Object



31
32
33
34
# File 'lib/agent99/timestamp.rb', line 31

def ts2utc(microseconds)
  # NOTE: This is an order of magitude faster than not bit shifting
  Time.at(microseconds >> 20, microseconds & 0xFFFFF).utc # Masking to get the last 20 bits  
end

.utc2ts(now = Time.now.utc) ⇒ Object



26
27
28
29
# File 'lib/agent99/timestamp.rb', line 26

def utc2ts(now=Time.now.utc)
  # NOTE: This is faster that bit shifting by the smallest of measures
  now.to_i * 1_000_000 + now.usec
end

Instance Method Details

#to_iObject



22
# File 'lib/agent99/timestamp.rb', line 22

def to_i    = @ts

#to_utcObject



23
# File 'lib/agent99/timestamp.rb', line 23

def to_utc  = @utc