Module: Datadog::Core::Utils::Time
- Defined in:
- lib/datadog/core/utils/time.rb
Overview
Common database-related utility functions.
Class Method Summary collapse
- .as_utc_epoch_ns(time) ⇒ Object
-
.get_time(unit = :float_second) ⇒ Numeric
Current monotonic time.
- .measure(unit = :float_second) ⇒ Object
-
.now ⇒ Time
Current wall time.
-
.now_provider=(block) ⇒ Object
Overrides the implementation of ‘#now with the provided callable.
Class Method Details
.as_utc_epoch_ns(time) ⇒ Object
44 45 46 47 48 |
# File 'lib/datadog/core/utils/time.rb', line 44 def as_utc_epoch_ns(time) # we use #to_r instead of #to_f because Float doesn't have enough precision to represent exact nanoseconds, see # https://rubyapi.org/3.0/o/time#method-i-to_f (time.to_r * 1_000_000_000).to_i end |
.get_time(unit = :float_second) ⇒ Numeric
Current monotonic time
14 15 16 |
# File 'lib/datadog/core/utils/time.rb', line 14 def get_time(unit = :float_second) Process.clock_gettime(Process::CLOCK_MONOTONIC, unit) end |
.measure(unit = :float_second) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/datadog/core/utils/time.rb', line 37 def measure(unit = :float_second) before = get_time(unit) yield after = get_time(unit) after - before end |
.now ⇒ Time
Current wall time.
21 22 23 |
# File 'lib/datadog/core/utils/time.rb', line 21 def now ::Time.now end |
.now_provider=(block) ⇒ Object
Overrides the implementation of ‘#now with the provided callable.
Overriding the method ‘#now` instead of indirectly calling `block` removes one level of method call overhead.
33 34 35 |
# File 'lib/datadog/core/utils/time.rb', line 33 def now_provider=(block) define_singleton_method(:now, &block) end |