Sha256: f754803b27a1c2c5c321d046fbbb95447967db3817769a72165f1ebe167c69f6

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

require 'concurrent/synchronization/object'
require 'concurrent/utility/monotonic_time'

module Concurrent
  class Channel

    # 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 Concurrent.monotonic_time
    # @see Concurrent::Channel.ticker
    # @see Concurrent::Channel.timer
    class Tick < Synchronization::Object
      include Comparable
      safe_initialization!

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

      attr_reader :monotonic, :utc

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

      def epoch
        @utc.to_f
      end

      def to_s
        @utc.strftime(STRING_FORMAT)
      end

      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

      private

      def monotonic_to_utc(tick)
        Time.now.utc + Concurrent.monotonic_time - tick
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
concurrent-ruby-edge-0.7.2 lib/concurrent-ruby-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.7.1 lib/concurrent-ruby-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.7.0 lib/concurrent-ruby-edge/concurrent/channel/tick.rb