Class: Monotonic::Time
- Inherits:
-
Object
- Object
- Monotonic::Time
- Defined in:
- lib/Monotonic/Time.rb
Constant Summary collapse
- CLOCK =
Read upon every instance, so a constant rather than a lookup. It follows .clock_name, and so must come after it.
Process.const_get(clock_name)
Instance Attribute Summary collapse
-
#nanoseconds_since_boot ⇒ Object
readonly
Returns the value of attribute nanoseconds_since_boot.
Class Method Summary collapse
-
.clock_name ⇒ Object
An instant, which #to_time maps back onto the wall clock by way of the boot time, so the clock wanted here is the one which tracks time since boot as the wall clock understands it.
- .now ⇒ Object
-
.resolution ⇒ Object
How finely this clock advances, in nanoseconds.
Instance Method Summary collapse
- #+(monotonic_time_addend) ⇒ Object
- #-(monotonic_time_subtrahend) ⇒ Object
-
#initialize ⇒ Time
constructor
A new instance of Time.
-
#seconds_since_boot ⇒ Object
The clock is read in nanoseconds because it answers there with an Integer, which is exact and stays exact however long the machine has been up.
- #to_s ⇒ Object
- #to_time ⇒ Object
Constructor Details
Instance Attribute Details
#nanoseconds_since_boot ⇒ Object (readonly)
Returns the value of attribute nanoseconds_since_boot.
42 43 44 |
# File 'lib/Monotonic/Time.rb', line 42 def nanoseconds_since_boot @nanoseconds_since_boot end |
Class Method Details
.clock_name ⇒ Object
An instant, which #to_time maps back onto the wall clock by way of the boot time, so the clock wanted here is the one which tracks time since boot as the wall clock understands it. That is CLOCK_MONOTONIC, sleep and all. Monotonic::Timer measures intervals rather than instants and chooses a finer clock of its own.
Unlike Timer's, this one is chosen rather than found, there being no alternative which would still answer to #to_time. It is a method all the same, so that the two classes answer the question alike.
20 21 22 |
# File 'lib/Monotonic/Time.rb', line 20 def clock_name :CLOCK_MONOTONIC end |
.now ⇒ Object
24 25 26 |
# File 'lib/Monotonic/Time.rb', line 24 def now self.new end |
.resolution ⇒ Object
How finely this clock advances, in nanoseconds. It is asked rather than tabulated, being a property of the processor and the operating system and not of this library: upon macOS CLOCK_MONOTONIC answers 1000 here. A reading is denominated in nanoseconds whatever the answer, so this is the method which says what a reading is worth.
33 34 35 |
# File 'lib/Monotonic/Time.rb', line 33 def resolution Process.clock_getres(CLOCK, :nanosecond) end |
Instance Method Details
#+(monotonic_time_addend) ⇒ Object
57 58 59 |
# File 'lib/Monotonic/Time.rb', line 57 def +(monotonic_time_addend) seconds_since_boot + monotonic_time_addend.seconds_since_boot end |
#-(monotonic_time_subtrahend) ⇒ Object
61 62 63 |
# File 'lib/Monotonic/Time.rb', line 61 def -(monotonic_time_subtrahend) seconds_since_boot - monotonic_time_subtrahend.seconds_since_boot end |
#seconds_since_boot ⇒ Object
The clock is read in nanoseconds because it answers there with an Integer, which is exact and stays exact however long the machine has been up. Seconds are derived rather than read, so that the reading loses nothing and the rounding happens where it is asked for.
48 49 50 |
# File 'lib/Monotonic/Time.rb', line 48 def seconds_since_boot @nanoseconds_since_boot / NANOSECONDS_PER_SECOND.to_f end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/Monotonic/Time.rb', line 65 def to_s "#{seconds_since_boot} seconds since boot." end |
#to_time ⇒ Object
69 70 71 |
# File 'lib/Monotonic/Time.rb', line 69 def to_time @boot_time + seconds_since_boot end |