Class: Concurrent::Channel::Tick

Inherits:
Synchronization::Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/concurrent/channel/tick.rb

Overview

A convenience class representing a single moment in monotonic time. Returned by Concurrent::Channel tickers and timers when they resolve.

Includes ‘Comparable` and can be compared to monotonic_time, UTC time, or epoch time.

See Also:

Constant Summary collapse

STRING_FORMAT =
'%F %T.%6N %z %Z'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tick = Concurrent.monotonic_time) ⇒ Tick

Returns a new instance of Tick.



25
26
27
28
# File 'lib/concurrent/channel/tick.rb', line 25

def initialize(tick = Concurrent.monotonic_time)
  @monotonic = tick
  @utc = monotonic_to_utc(tick).freeze
end

Instance Attribute Details

#monotonicObject (readonly)

Returns the value of attribute monotonic.



23
24
25
# File 'lib/concurrent/channel/tick.rb', line 23

def monotonic
  @monotonic
end

#utcObject (readonly)

Returns the value of attribute utc.



23
24
25
# File 'lib/concurrent/channel/tick.rb', line 23

def utc
  @utc
end

Instance Method Details

#<=>(other) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/concurrent/channel/tick.rb', line 38

def <=>(other)
  if other.is_a? Numeric
    @monotonic <=> other
  elsif other.is_a? Time
    @utc <=> other.utc
  elsif other.is_a? Tick
    @monotonic <=> other.monotonic
  else
    nil
  end
end

#epochObject



30
31
32
# File 'lib/concurrent/channel/tick.rb', line 30

def epoch
  @utc.to_f
end

#to_sObject



34
35
36
# File 'lib/concurrent/channel/tick.rb', line 34

def to_s
  @utc.strftime(STRING_FORMAT)
end