Class: Monotonic::Time

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTime

Returns a new instance of Time.



52
53
54
55
# File 'lib/Monotonic/Time.rb', line 52

def initialize
  @boot_time = Sys::Uptime.boot_time
  @nanoseconds_since_boot = Process.clock_gettime(CLOCK, :nanosecond)
end

Instance Attribute Details

#nanoseconds_since_bootObject (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_nameObject

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

.nowObject



24
25
26
# File 'lib/Monotonic/Time.rb', line 24

def now
  self.new
end

.resolutionObject

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_bootObject

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_sObject



65
66
67
# File 'lib/Monotonic/Time.rb', line 65

def to_s
  "#{seconds_since_boot} seconds since boot."
end

#to_timeObject



69
70
71
# File 'lib/Monotonic/Time.rb', line 69

def to_time
  @boot_time + seconds_since_boot
end