Class: Monotime::Instant
- Inherits:
-
Object
- Object
- Monotime::Instant
- Includes:
- Comparable
- Defined in:
- lib/monotime/instant.rb
Overview
A measurement from the operating system’s monotonic clock, with up to nanosecond precision.
Class Method Summary collapse
-
.now ⇒ Instant
An alias to
new, and generally preferred over it.
Instance Method Summary collapse
-
#+(other) ⇒ Instant
Add a
Durationor#to_nanos-coercible object to thisInstant, returning a newInstant. -
#-(other) ⇒ Duration, Instant
Subtract another
Instantto generate aDurationbetween the two, or aDurationor#to_nanos-coercible object, to generate anInstantoffset by it. -
#<=>(other) ⇒ -1, ...
Determine if the given
Instantis before, equal to or after this one. -
#==(other) ⇒ Boolean
(also: #eql?)
Determine if
other‘s value equals that of thisInstant. -
#duration_since(earlier) ⇒ Duration
Return a
Durationbetween thisInstantand another. -
#elapsed ⇒ Duration
Return a
Durationsince thisInstantand now. -
#hash ⇒ Integer
Generate a hash for this type and value.
-
#in_future? ⇒ Boolean
(also: #future?)
Return whether this
Instantis in the future. -
#in_past? ⇒ Boolean
(also: #past?)
Return whether this
Instantis in the past. -
#initialize(nanos = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)) ⇒ Instant
constructor
Create a new
Instantfrom an optional nanosecond measurement. -
#sleep(duration = nil) ⇒ Duration
Sleep until this
Instant, plus an optionalDuration, returning aDurationthat’s either positive if any time was slept, or negative if sleeping would require time travel. -
#sleep_millis(millis) ⇒ Duration
Sleep for the given number of milliseconds past this
Instant, if any. -
#sleep_secs(secs) ⇒ Duration
Sleep for the given number of seconds past this
Instant, if any. -
#to_s(*args) ⇒ Object
Sugar for
#elapsed.to_s.
Constructor Details
#initialize(nanos = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)) ⇒ Instant
Create a new Instant from an optional nanosecond measurement.
Users should generally not pass anything to this function.
20 21 22 |
# File 'lib/monotime/instant.rb', line 20 def initialize(nanos = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)) @ns = Integer(nanos) end |
Class Method Details
.now ⇒ Instant
An alias to new, and generally preferred over it.
27 28 29 |
# File 'lib/monotime/instant.rb', line 27 def self.now new end |
Instance Method Details
#+(other) ⇒ Instant
Add a Duration or #to_nanos-coercible object to this Instant, returning a new Instant.
126 127 128 129 130 |
# File 'lib/monotime/instant.rb', line 126 def +(other) return TypeError, 'Not one of: [Duration, #to_nanos]' unless other.respond_to?(:to_nanos) Instant.new(@ns + other.to_nanos) end |
#-(other) ⇒ Duration, Instant
Subtract another Instant to generate a Duration between the two, or a Duration or #to_nanos-coercible object, to generate an Instant offset by it.
142 143 144 145 146 147 148 149 150 |
# File 'lib/monotime/instant.rb', line 142 def -(other) if other.is_a?(Instant) Duration.new(@ns - other.ns) elsif other.respond_to?(:to_nanos) Instant.new(@ns - other.to_nanos) else raise TypeError, 'Not one of: [Instant, Duration, #to_nanos]' end end |
#<=>(other) ⇒ -1, ...
Determine if the given Instant is before, equal to or after this one. nil if not passed an Instant.
156 157 158 |
# File 'lib/monotime/instant.rb', line 156 def <=>(other) @ns <=> other.ns if other.is_a?(Instant) end |
#==(other) ⇒ Boolean Also known as: eql?
Determine if other‘s value equals that of this Instant. Use eql? if type checks are desired for future compatibility.
165 166 167 |
# File 'lib/monotime/instant.rb', line 165 def ==(other) other.is_a?(Instant) && @ns == other.ns end |
#duration_since(earlier) ⇒ Duration
Return a Duration between this Instant and another.
35 36 37 38 39 |
# File 'lib/monotime/instant.rb', line 35 def duration_since(earlier) raise TypeError, 'Not an Instant' unless earlier.is_a?(Instant) earlier - self end |
#elapsed ⇒ Duration
Return a Duration since this Instant and now.
44 45 46 |
# File 'lib/monotime/instant.rb', line 44 def elapsed duration_since(self.class.now) end |
#hash ⇒ Integer
Generate a hash for this type and value.
174 175 176 |
# File 'lib/monotime/instant.rb', line 174 def hash self.class.hash ^ @ns.hash end |
#in_future? ⇒ Boolean Also known as: future?
Return whether this Instant is in the future.
60 61 62 |
# File 'lib/monotime/instant.rb', line 60 def in_future? elapsed.negative? end |
#in_past? ⇒ Boolean Also known as: past?
Return whether this Instant is in the past.
51 52 53 |
# File 'lib/monotime/instant.rb', line 51 def in_past? elapsed.positive? end |
#sleep(duration = nil) ⇒ Duration
Sleep until this Instant, plus an optional Duration, returning a Duration that’s either positive if any time was slept, or negative if sleeping would require time travel.
83 84 85 86 87 |
# File 'lib/monotime/instant.rb', line 83 def sleep(duration = nil) remaining = duration ? duration - elapsed : -elapsed remaining.tap { |rem| rem.sleep if rem.positive? } end |
#sleep_millis(millis) ⇒ Duration
Sleep for the given number of milliseconds past this Instant, if any.
Equivalent to #sleep(Duration.from_millis(millis))
107 108 109 |
# File 'lib/monotime/instant.rb', line 107 def sleep_millis(millis) sleep(Duration.from_millis(millis)) end |
#sleep_secs(secs) ⇒ Duration
Sleep for the given number of seconds past this Instant, if any.
Equivalent to #sleep(Duration.from_secs(secs))
96 97 98 |
# File 'lib/monotime/instant.rb', line 96 def sleep_secs(secs) sleep(Duration.from_secs(secs)) end |
#to_s(*args) ⇒ Object
Sugar for #elapsed.to_s.
114 115 116 |
# File 'lib/monotime/instant.rb', line 114 def to_s(*args) elapsed.to_s(*args) end |