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.



437
438
439
440
441
442
443
444
445
446
447
# File 'lib/fluent/time.rb', line 437

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



453
454
455
456
457
458
459
460
461
462
# File 'lib/fluent/time.rb', line 453

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



449
450
451
# File 'lib/fluent/time.rb', line 449

def format_unixtime(time)
  time.to_i.to_s
end