Module: Fluent::Clock

Defined in:
lib/fluent/clock.rb

Constant Summary collapse

CLOCK_ID =
Process::CLOCK_MONOTONIC_RAW rescue Process::CLOCK_MONOTONIC
@@block_level =
0
@@frozen_clock =
nil

Class Method Summary collapse

Class Method Details

.dst_clock_from_time(time) ⇒ Object



50
51
52
53
# File 'lib/fluent/clock.rb', line 50

def self.dst_clock_from_time(time)
  diff_sec = Time.now - time
  now_raw - diff_sec
end

.freeze(dst = nil, &block) ⇒ Object



28
29
30
31
32
33
# File 'lib/fluent/clock.rb', line 28

def self.freeze(dst = nil, &block)
  return freeze_block(dst, &block) if block_given?

  dst = dst_clock_from_time(dst) if dst.is_a?(Time)
  @@frozen_clock = dst || now_raw
end

.freeze_block(dst) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/fluent/clock.rb', line 55

def self.freeze_block(dst)
  dst = dst_clock_from_time(dst) if dst.is_a?(Time)
  pre_frozen_clock = @@frozen_clock
  @@frozen_clock = dst || now_raw
  @@block_level += 1
  yield
ensure
  @@block_level -= 1
  @@frozen_clock = pre_frozen_clock
end

.nowObject



24
25
26
# File 'lib/fluent/clock.rb', line 24

def self.now
  @@frozen_clock || now_raw
end

.now_rawObject

internal use



42
43
44
# File 'lib/fluent/clock.rb', line 42

def self.now_raw
  Process.clock_gettime(CLOCK_ID)
end

.real_now(unit = :second) ⇒ Object



46
47
48
# File 'lib/fluent/clock.rb', line 46

def self.real_now(unit = :second)
  Process.clock_gettime(Process::CLOCK_REALTIME, unit)
end

.returnObject



35
36
37
38
# File 'lib/fluent/clock.rb', line 35

def self.return
  raise "invalid return while running code in blocks" if @@block_level > 0
  @@frozen_clock = nil
end