Sha256: 78bfca6134a111df9dc33b92c9bda9480e4ecc32d0f7bfe0cf9f7af91bc51f40

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

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
      include Comparable

      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

2 entries across 2 versions & 1 rubygems

Version Path
concurrent-ruby-edge-0.2.0.pre4 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.0.pre3 lib/concurrent/channel/tick.rb