Class: Fluent::EventTime

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/time.rb

Constant Summary collapse

TYPE =
0
FORMATTER =
Strftime.new('%Y-%m-%d %H:%M:%S.%N %z')

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sec, nsec = 0) ⇒ EventTime

Returns a new instance of EventTime.



29
30
31
32
# File 'lib/fluent/time.rb', line 29

def initialize(sec, nsec = 0)
  @sec = sec
  @nsec = nsec
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

TODO: For performance, implement +, -, and so on



125
126
127
# File 'lib/fluent/time.rb', line 125

def method_missing(name, *args, &block)
  @sec.send(name, *args, &block)
end

Class Method Details

.eq?(a, b) ⇒ Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
# File 'lib/fluent/time.rb', line 106

def self.eq?(a, b)
  if a.is_a?(Fluent::EventTime) && b.is_a?(Fluent::EventTime)
    a.sec == b.sec && a.nsec == b.nsec
  else
    a == b
  end
end

.from_msgpack_ext(data) ⇒ Object



98
99
100
# File 'lib/fluent/time.rb', line 98

def self.from_msgpack_ext(data)
  new(*data.unpack('NN'))
end

.from_time(time) ⇒ Object



102
103
104
# File 'lib/fluent/time.rb', line 102

def self.from_time(time)
  Fluent::EventTime.new(time.to_i, time.nsec)
end

.nowObject



114
115
116
117
118
# File 'lib/fluent/time.rb', line 114

def self.now
  # This method is called many time. so call Process.clock_gettime directly instead of Fluent::Clock.real_now
  now = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
  Fluent::EventTime.new(now / 1_000_000_000, now % 1_000_000_000)
end

.parse(*args) ⇒ Object



120
121
122
# File 'lib/fluent/time.rb', line 120

def self.parse(*args)
  from_time(Time.parse(*args))
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/fluent/time.rb', line 34

def ==(other)
  if other.is_a?(Fluent::EventTime)
    @sec == other.sec
  else
    @sec == other
  end
end

#coerce(other) ⇒ Object

for > and others



65
66
67
# File 'lib/fluent/time.rb', line 65

def coerce(other)
  [other, @sec]
end

#inspectObject



129
130
131
# File 'lib/fluent/time.rb', line 129

def inspect
  FORMATTER.exec(Time.at(self))
end

#nsecObject



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

def nsec
  @nsec
end

#secObject



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

def sec
  @sec
end

#to_fObject



55
56
57
# File 'lib/fluent/time.rb', line 55

def to_f
  @sec + @nsec / 1_000_000_000.0
end

#to_intObject Also known as: to_i



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

def to_int
  @sec
end

#to_json(*args) ⇒ Object



86
87
88
# File 'lib/fluent/time.rb', line 86

def to_json(*args)
  @sec.to_s
end

#to_msgpack(io = nil) ⇒ Object



90
91
92
# File 'lib/fluent/time.rb', line 90

def to_msgpack(io = nil)
  @sec.to_msgpack(io)
end

#to_msgpack_extObject



94
95
96
# File 'lib/fluent/time.rb', line 94

def to_msgpack_ext
  [@sec, @nsec].pack('NN')
end

#to_rObject

for Time.at



60
61
62
# File 'lib/fluent/time.rb', line 60

def to_r
  Rational(@sec * 1_000_000_000 + @nsec, 1_000_000_000)
end

#to_sObject



69
70
71
# File 'lib/fluent/time.rb', line 69

def to_s
  @sec.to_s
end