Sha256: 4df7e08ef9ff1f5f7ac205c29c59126101c3e687a20383cacd7922c29b35caab

Contents?: true

Size: 1.29 KB

Versions: 15

Compression:

Stored size: 1.29 KB

Contents

require 'concurrent/synchronization'
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

15 entries across 15 versions & 1 rubygems

Version Path
concurrent-ruby-edge-0.6.0 lib/concurrent-ruby-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.6.0.pre1 lib/concurrent-ruby-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.5.0 lib-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.4.1 lib-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.4.0.pre2 lib-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.4.0.pre1 lib-edge/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.4 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.3.1 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.3.0 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.3 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.3.pre3 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.2 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.1 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.0 lib/concurrent/channel/tick.rb
concurrent-ruby-edge-0.2.0.pre5 lib/concurrent/channel/tick.rb