Class: Fluent::NumericTimeFormatter

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

Instance Method Summary collapse

Methods inherited from TimeFormatter

#format_nocache, #format_with_subsec, #format_without_subsec

Constructor Details

#initialize(type, localtime = nil, timezone = nil) ⇒ NumericTimeFormatter

Returns a new instance of NumericTimeFormatter.



379
380
381
382
383
384
385
386
387
388
389
# File 'lib/fluent/time.rb', line 379

def initialize(type, localtime = nil, timezone = nil)
  @cache1_key = @cache1_time = @cache2_key = @cache2_time = nil

  if type == :unixtime
    define_singleton_method(:format, method(:format_unixtime))
    define_singleton_method(:call, method(:format_unixtime))
  else # :float
    define_singleton_method(:format, method(:format_float))
    define_singleton_method(:call, method(:format_float))
  end
end

Instance Method Details

#format_float(time) ⇒ Object



395
396
397
398
399
400
401
402
403
404
# File 'lib/fluent/time.rb', line 395

def format_float(time)
  if time.is_a?(Fluent::EventTime) || time.is_a?(Time)
    # 10.015 secs for 10_000_000 times call on MacBookAir
    nsec_s = time.nsec.to_s
    nsec_s = '0' * (9 - nsec_s.size) if nsec_s.size < 9
    "#{time.sec}.#{nsec_s}"
  else # integer (or float?)
    time.to_f.to_s
  end
end

#format_unixtime(time) ⇒ Object



391
392
393
# File 'lib/fluent/time.rb', line 391

def format_unixtime(time)
  time.to_i.to_s
end