Class: Monotime::Duration
- Inherits:
-
Object
- Object
- Monotime::Duration
- Includes:
- Comparable
- Defined in:
- lib/monotime/duration.rb
Overview
A type representing a span of time in nanoseconds.
Constant Summary collapse
- ZERO =
A static instance for zero durations
allocate.tap { |d| d.__send__(:initialize, 0) }
Class Attribute Summary collapse
-
.default_to_s_precision(precision) ⇒ Object
Precision for Duration#to_s if not otherwise specified.
-
.sleep_function(function) ⇒ Object
The sleep function used by all
Monotime
sleep functions.
Class Method Summary collapse
-
.from_micros(micros) ⇒ Duration
(also: micros)
Generate a new
Duration
measuring the given number of microseconds. -
.from_millis(millis) ⇒ Duration
(also: millis)
Generate a new
Duration
measuring the given number of milliseconds. -
.from_nanos(nanos) ⇒ Duration
(also: nanos)
Generate a new
Duration
measuring the given number of nanoseconds. -
.from_secs(secs) ⇒ Duration
(also: secs)
Generate a new
Duration
measuring the given number of seconds. -
.measure ⇒ Duration
Return a
Duration
measuring the elapsed time of the yielded block. -
.with_measure ⇒ Object, Duration
Return the result of the yielded block alongside a
Duration
. -
.zero ⇒ Duration
Return a zero
Duration
.
Instance Method Summary collapse
-
#*(other) ⇒ Duration
Multiply this duration by a
Numeric
. -
#+(other) ⇒ Duration
Add another
Duration
or#to_nanos
-coercible object to this one, returning a newDuration
. -
#-(other) ⇒ Duration
Subtract another
Duration
or#to_nanos
-coercible object from this one, returning a newDuration
. -
#-@ ⇒ Duration
Unary minus: make a positive
Duration
negative, and vice versa. -
#/(other) ⇒ Duration
Divide this duration by a
Numeric
. -
#<=>(other) ⇒ -1, ...
Compare the value of this
Duration
with another, or any#to_nanos
-coercible object, or nil if not comparable. -
#==(other) ⇒ Boolean
Compare the equality of the value of this
Duration
with another, or any#to_nanos
-coercible object, or nil if not comparable. -
#abs ⇒ Duration
Return a
Duration
that’s absolute (positive). -
#eql?(other) ⇒ Boolean
Check equality of the value and type of this
Duration
with another. -
#hash ⇒ Integer
Generate a hash for this type and value.
-
#initialize(nanos = 0) ⇒ Duration
constructor
Create a new
Duration
of a specified number of nanoseconds, zero by default. -
#negative? ⇒ Boolean
Return true if this
Duration
is negative. -
#nonzero? ⇒ Boolean
Return true if this
Duration
is non-zero. -
#positive? ⇒ Boolean
Return true if this
Duration
is positive. -
#sleep ⇒ Integer
Sleep for the duration of this
Duration
. -
#to_micros ⇒ Float
(also: #micros)
Return this
Duration
in microseconds. -
#to_millis ⇒ Float
(also: #millis)
Return this
Duration
in milliseconds. -
#to_nanos ⇒ Integer
(also: #nanos)
Return this
Duration
in nanoseconds. -
#to_s(precision = self.class.default_to_s_precision) ⇒ String
Format this
Duration
into a human-readable string, with a given number of decimal places. -
#to_secs ⇒ Float
(also: #secs)
Return this
Duration
in seconds. -
#zero? ⇒ Boolean
Return true if this
Duration
is zero.
Constructor Details
#initialize(nanos = 0) ⇒ Duration
Create a new Duration
of a specified number of nanoseconds, zero by default.
Users are strongly advised to use Duration.from_nanos
instead.
15 16 17 18 |
# File 'lib/monotime/duration.rb', line 15 def initialize(nanos = 0) @ns = Integer(nanos) freeze end |
Class Attribute Details
.default_to_s_precision=(precision) ⇒ Object
Precision for Duration#to_s if not otherwise specified
Defaults to 9.
41 42 43 |
# File 'lib/monotime/duration.rb', line 41 def default_to_s_precision @default_to_s_precision end |
.sleep_function=(function) ⇒ Object
The sleep function used by all Monotime
sleep functions.
This function must accept a positive Float
number of seconds and return the Float
time slept.
Defaults to Kernel.method(:sleep)
33 34 35 |
# File 'lib/monotime/duration.rb', line 33 def sleep_function @sleep_function end |
Class Method Details
.from_micros(micros) ⇒ Duration Also known as: micros
Generate a new Duration
measuring the given number of microseconds.
86 87 88 |
# File 'lib/monotime/duration.rb', line 86 def from_micros(micros) new(Integer(micros * 1_000)) end |
.from_millis(millis) ⇒ Duration Also known as: millis
Generate a new Duration
measuring the given number of milliseconds.
76 77 78 |
# File 'lib/monotime/duration.rb', line 76 def from_millis(millis) new(Integer(millis * 1_000_000)) end |
.from_nanos(nanos) ⇒ Duration Also known as: nanos
Generate a new Duration
measuring the given number of nanoseconds.
96 97 98 |
# File 'lib/monotime/duration.rb', line 96 def from_nanos(nanos) new(Integer(nanos)) end |
.from_secs(secs) ⇒ Duration Also known as: secs
Generate a new Duration
measuring the given number of seconds.
66 67 68 |
# File 'lib/monotime/duration.rb', line 66 def from_secs(secs) new(Integer(secs * 1_000_000_000)) end |
.measure ⇒ Duration
Return a Duration
measuring the elapsed time of the yielded block.
108 109 110 111 112 |
# File 'lib/monotime/duration.rb', line 108 def measure start = Instant.now yield start.elapsed end |
Instance Method Details
#*(other) ⇒ Duration
Multiply this duration by a Numeric
.
173 174 175 |
# File 'lib/monotime/duration.rb', line 173 def *(other) Duration.new(to_nanos * other) end |
#+(other) ⇒ Duration
Add another Duration
or #to_nanos
-coercible object to this one, returning a new Duration
.
135 136 137 138 139 |
# File 'lib/monotime/duration.rb', line 135 def +(other) raise TypeError, 'Not one of: [Duration, #to_nanos]' unless other.respond_to?(:to_nanos) Duration.new(to_nanos + other.to_nanos) end |
#-(other) ⇒ Duration
Subtract another Duration
or #to_nanos
-coercible object from this one, returning a new Duration
.
149 150 151 152 153 |
# File 'lib/monotime/duration.rb', line 149 def -(other) raise TypeError, 'Not one of: [Duration, #to_nanos]' unless other.respond_to?(:to_nanos) Duration.new(to_nanos - other.to_nanos) end |
#-@ ⇒ Duration
Unary minus: make a positive Duration
negative, and vice versa.
184 185 186 |
# File 'lib/monotime/duration.rb', line 184 def -@ Duration.new(-to_nanos) end |
#/(other) ⇒ Duration
Divide this duration by a Numeric
.
162 163 164 |
# File 'lib/monotime/duration.rb', line 162 def /(other) Duration.new(to_nanos / other) end |
#<=>(other) ⇒ -1, ...
Compare the value of this Duration
with another, or any #to_nanos
-coercible object, or nil if not comparable.
206 207 208 |
# File 'lib/monotime/duration.rb', line 206 def <=>(other) to_nanos <=> other.to_nanos if other.respond_to?(:to_nanos) end |
#==(other) ⇒ Boolean
Compare the equality of the value of this Duration
with another, or any #to_nanos
-coercible object, or nil if not comparable.
215 216 217 |
# File 'lib/monotime/duration.rb', line 215 def ==(other) other.respond_to?(:to_nanos) && to_nanos == other.to_nanos end |
#abs ⇒ Duration
Return a Duration
that’s absolute (positive).
195 196 197 198 199 |
# File 'lib/monotime/duration.rb', line 195 def abs return self if positive? || zero? Duration.new(to_nanos.abs) end |
#eql?(other) ⇒ Boolean
Check equality of the value and type of this Duration
with another.
223 224 225 |
# File 'lib/monotime/duration.rb', line 223 def eql?(other) other.is_a?(Duration) && to_nanos == other.to_nanos end |
#hash ⇒ Integer
Generate a hash for this type and value.
230 231 232 |
# File 'lib/monotime/duration.rb', line 230 def hash [self.class, to_nanos].hash end |
#negative? ⇒ Boolean
Return true if this Duration
is negative.
280 281 282 |
# File 'lib/monotime/duration.rb', line 280 def negative? to_nanos.negative? end |
#nonzero? ⇒ Boolean
Return true if this Duration
is non-zero.
294 295 296 |
# File 'lib/monotime/duration.rb', line 294 def nonzero? to_nanos.nonzero? end |
#positive? ⇒ Boolean
Return true if this Duration
is positive.
273 274 275 |
# File 'lib/monotime/duration.rb', line 273 def positive? to_nanos.positive? end |
#sleep ⇒ Integer
Sleep for the duration of this Duration
. Equivalent to Kernel.sleep(duration.to_secs).
The sleep function may be overridden globally using Duration.sleep_function=
311 312 313 314 315 |
# File 'lib/monotime/duration.rb', line 311 def sleep raise NotImplementedError, 'time travel module missing' if negative? self.class.sleep_function.call(to_secs) end |
#to_micros ⇒ Float Also known as: micros
Return this Duration
in microseconds.
255 256 257 |
# File 'lib/monotime/duration.rb', line 255 def to_micros to_nanos / 1_000.0 end |
#to_millis ⇒ Float Also known as: millis
Return this Duration
in milliseconds.
246 247 248 |
# File 'lib/monotime/duration.rb', line 246 def to_millis to_nanos / 1_000_000.0 end |
#to_nanos ⇒ Integer Also known as: nanos
Return this Duration
in nanoseconds.
264 265 266 |
# File 'lib/monotime/duration.rb', line 264 def to_nanos @ns end |
#to_s(precision = self.class.default_to_s_precision) ⇒ String
Format this Duration
into a human-readable string, with a given number of decimal places.
The default precision may be set globally using Duration.default_to_s_precision=
The exact format is subject to change, users with specific requirements are encouraged to use their own formatting methods.
345 346 347 348 349 350 351 352 353 354 |
# File 'lib/monotime/duration.rb', line 345 def to_s(precision = self.class.default_to_s_precision) precision = Integer(precision).abs div, unit = DIVISORS.find { |d, _| to_nanos.abs >= d } if div.zero? format('%d%s', to_nanos, unit) else format("%#.#{precision}f", to_nanos / div).sub(/\.?0*\z/, '') << unit end end |
#to_secs ⇒ Float Also known as: secs
Return this Duration
in seconds.
237 238 239 |
# File 'lib/monotime/duration.rb', line 237 def to_secs to_nanos / 1_000_000_000.0 end |
#zero? ⇒ Boolean
Return true if this Duration
is zero.
287 288 289 |
# File 'lib/monotime/duration.rb', line 287 def zero? to_nanos.zero? end |