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 Attribute Summary collapse
- .clock_id ⇒ Object
-
.monotonic_function(function) ⇒ Object
The function used to create
Instant
instances.
Class Method Summary collapse
-
.clock_getres(clock = clock_id) ⇒ Object
Return the claimed resolution of the given clock id or the configured
clock_id
, as aDuration
, ornil
if invalid. -
.clock_name ⇒ Symbol?
The symbolic name of the currently-selected
clock_id
, if available. -
.now ⇒ Instant
An alias to
new
, and generally preferred over it.
Instance Method Summary collapse
-
#+(other) ⇒ Instant
Add a
Duration
or#to_nanos
-coercible object to thisInstant
, returning a newInstant
. -
#-(other) ⇒ Duration, Instant
Subtract another
Instant
to generate aDuration
between the two, or aDuration
or#to_nanos
-coercible object, to generate anInstant
offset by it. -
#<=>(other) ⇒ -1, ...
Determine if the given
Instant
is 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
Duration
between thisInstant
and another. -
#elapsed ⇒ Duration
Return a
Duration
since thisInstant
and now. -
#hash ⇒ Integer
Generate a hash for this type and value.
-
#in_future? ⇒ Boolean
(also: #future?)
Return whether this
Instant
is in the future. -
#in_past? ⇒ Boolean
(also: #past?)
Return whether this
Instant
is in the past. -
#initialize(nanos = self.class.monotonic_function.call) ⇒ Instant
constructor
Create a new
Instant
from an optional nanosecond measurement. -
#sleep(duration = nil) ⇒ Duration
Sleep until this
Instant
, plus an optionalDuration
, returning aDuration
that’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 ⇒ Object
Sugar for
#elapsed.to_s
.
Constructor Details
#initialize(nanos = self.class.monotonic_function.call) ⇒ Instant
Create a new Instant
from an optional nanosecond measurement.
Users should generally not pass anything to this function.
120 121 122 123 |
# File 'lib/monotime/instant.rb', line 120 def initialize(nanos = self.class.monotonic_function.call) @ns = Integer(nanos) freeze end |
Class Attribute Details
.clock_id ⇒ Numeric .clock_id=(id) ⇒ Object
52 53 54 |
# File 'lib/monotime/instant.rb', line 52 def clock_id @clock_id end |
.monotonic_function=(function) ⇒ Object
The function used to create Instant
instances.
This function must return a Numeric
, monotonic count of nanoseconds since a fixed point in the past.
Defaults to -> { Process.clock_gettime(clock_id, :nanosecond) }.
63 64 65 |
# File 'lib/monotime/instant.rb', line 63 def monotonic_function @monotonic_function end |
Class Method Details
.clock_getres(clock = clock_id) ⇒ Object
Return the claimed resolution of the given clock id or the configured clock_id
, as a Duration
, or nil
if invalid.
Note per Ruby issue #16740, the practical usability of this method is dubious and non-portable.
72 73 74 75 76 |
# File 'lib/monotime/instant.rb', line 72 def clock_getres(clock = clock_id) Duration.from_nanos(Process.clock_getres(clock, :nanosecond)) rescue SystemCallError # suppress errors end |
.clock_name ⇒ Symbol?
The symbolic name of the currently-selected clock_id
, if available.
81 82 83 84 85 86 87 |
# File 'lib/monotime/instant.rb', line 81 def clock_name return clock_id if clock_id.is_a? Symbol Process.constants.find do |c| c.to_s.start_with?('CLOCK_') && Process.const_get(c) == clock_id end end |
.now ⇒ Instant
An alias to new
, and generally preferred over it.
128 129 130 |
# File 'lib/monotime/instant.rb', line 128 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
.
227 228 229 230 231 |
# File 'lib/monotime/instant.rb', line 227 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.
243 244 245 246 247 248 249 250 251 |
# File 'lib/monotime/instant.rb', line 243 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
.
257 258 259 |
# File 'lib/monotime/instant.rb', line 257 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.
266 267 268 |
# File 'lib/monotime/instant.rb', line 266 def ==(other) other.is_a?(Instant) && @ns == other.ns end |
#duration_since(earlier) ⇒ Duration
Return a Duration
between this Instant
and another.
136 137 138 139 140 |
# File 'lib/monotime/instant.rb', line 136 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.
145 146 147 |
# File 'lib/monotime/instant.rb', line 145 def elapsed duration_since(self.class.now) end |
#hash ⇒ Integer
Generate a hash for this type and value.
275 276 277 |
# File 'lib/monotime/instant.rb', line 275 def hash [self.class, @ns].hash end |
#in_future? ⇒ Boolean Also known as: future?
Return whether this Instant
is in the future.
161 162 163 |
# File 'lib/monotime/instant.rb', line 161 def in_future? elapsed.negative? end |
#in_past? ⇒ Boolean Also known as: past?
Return whether this Instant
is in the past.
152 153 154 |
# File 'lib/monotime/instant.rb', line 152 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.
184 185 186 187 188 |
# File 'lib/monotime/instant.rb', line 184 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))
208 209 210 |
# File 'lib/monotime/instant.rb', line 208 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))
197 198 199 |
# File 'lib/monotime/instant.rb', line 197 def sleep_secs(secs) sleep(Duration.from_secs(secs)) end |
#to_s ⇒ Object
Sugar for #elapsed.to_s
.
215 216 217 |
# File 'lib/monotime/instant.rb', line 215 def to_s(...) elapsed.to_s(...) end |