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.
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
.
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 = 9) ⇒ 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 #from_nanos
instead.
15 16 17 18 |
# File 'lib/monotime/duration.rb', line 15 def initialize(nanos = 0) @ns = Integer(nanos) freeze end |
Class Method Details
.from_micros(micros) ⇒ Duration Also known as: micros
Generate a new Duration
measuring the given number of microseconds.
45 46 47 |
# File 'lib/monotime/duration.rb', line 45 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.
35 36 37 |
# File 'lib/monotime/duration.rb', line 35 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.
55 56 57 |
# File 'lib/monotime/duration.rb', line 55 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.
25 26 27 |
# File 'lib/monotime/duration.rb', line 25 def from_secs(secs) new(Integer(secs * 1_000_000_000)) end |
Instance Method Details
#*(other) ⇒ Duration
Multiply this duration by a Numeric
.
130 131 132 |
# File 'lib/monotime/duration.rb', line 130 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
.
92 93 94 95 96 |
# File 'lib/monotime/duration.rb', line 92 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
.
106 107 108 109 110 |
# File 'lib/monotime/duration.rb', line 106 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.
141 142 143 |
# File 'lib/monotime/duration.rb', line 141 def -@ Duration.new(-to_nanos) end |
#/(other) ⇒ Duration
Divide this duration by a Numeric
.
119 120 121 |
# File 'lib/monotime/duration.rb', line 119 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.
163 164 165 |
# File 'lib/monotime/duration.rb', line 163 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.
172 173 174 |
# File 'lib/monotime/duration.rb', line 172 def ==(other) other.respond_to?(:to_nanos) && to_nanos == other.to_nanos end |
#abs ⇒ Duration
Return a Duration
that’s absolute (positive).
152 153 154 155 156 |
# File 'lib/monotime/duration.rb', line 152 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.
180 181 182 |
# File 'lib/monotime/duration.rb', line 180 def eql?(other) other.is_a?(Duration) && to_nanos == other.to_nanos end |
#hash ⇒ Integer
Generate a hash for this type and value.
187 188 189 |
# File 'lib/monotime/duration.rb', line 187 def hash self.class.hash ^ to_nanos.hash end |
#negative? ⇒ Boolean
Return true if this Duration
is negative.
237 238 239 |
# File 'lib/monotime/duration.rb', line 237 def negative? to_nanos.negative? end |
#nonzero? ⇒ Boolean
Return true if this Duration
is non-zero.
251 252 253 |
# File 'lib/monotime/duration.rb', line 251 def nonzero? to_nanos.nonzero? end |
#positive? ⇒ Boolean
Return true if this Duration
is positive.
230 231 232 |
# File 'lib/monotime/duration.rb', line 230 def positive? to_nanos.positive? end |
#sleep ⇒ Integer
Sleep for the duration of this Duration
. Equivalent to Kernel.sleep(duration.to_secs).
265 266 267 268 269 |
# File 'lib/monotime/duration.rb', line 265 def sleep raise NotImplementedError, 'time travel module missing' if negative? Kernel.sleep(to_secs) end |
#to_micros ⇒ Float Also known as: micros
Return this Duration
in microseconds.
212 213 214 |
# File 'lib/monotime/duration.rb', line 212 def to_micros to_nanos / 1_000.0 end |
#to_millis ⇒ Float Also known as: millis
Return this Duration
in milliseconds.
203 204 205 |
# File 'lib/monotime/duration.rb', line 203 def to_millis to_nanos / 1_000_000.0 end |
#to_nanos ⇒ Integer Also known as: nanos
Return this Duration
in nanoseconds.
221 222 223 |
# File 'lib/monotime/duration.rb', line 221 def to_nanos @ns end |
#to_s(precision = 9) ⇒ String
Format this Duration
into a human-readable string, with a given number of decimal places.
The exact format is subject to change, users with specific requirements are encouraged to use their own formatting methods.
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/monotime/duration.rb', line 296 def to_s(precision = 9) 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.
194 195 196 |
# File 'lib/monotime/duration.rb', line 194 def to_secs to_nanos / 1_000_000_000.0 end |
#zero? ⇒ Boolean
Return true if this Duration
is zero.
244 245 246 |
# File 'lib/monotime/duration.rb', line 244 def zero? to_nanos.zero? end |