class Fluent::TimeFormatter

Public Class Methods

new(format = nil, localtime = true, timezone = nil) click to toggle source
# File lib/fluent/time.rb, line 364
def initialize(format = nil, localtime = true, timezone = nil)
  @tc1 = 0
  @tc1_str = nil
  @tc2 = 0
  @tc2_str = nil

  strftime = format && (Strftime.new(format) rescue nil)
  if format && format =~ /(^|[^%])(%%)*%L|(^|[^%])(%%)*%\d*N/
    define_singleton_method(:format, method(:format_with_subsec))
    define_singleton_method(:call, method(:format_with_subsec))
  else
    define_singleton_method(:format, method(:format_without_subsec))
    define_singleton_method(:call, method(:format_without_subsec))
  end

  formatter = Fluent::Timezone.formatter(timezone, strftime ? strftime : format)
  @format_nocache = case
                    when formatter             then formatter
                    when strftime && localtime then ->(time){ strftime.exec(Time.at(time)) }
                    when format && localtime   then ->(time){ Time.at(time).strftime(format) }
                    when strftime              then ->(time){ strftime.exec(Time.at(time).utc) }
                    when format                then ->(time){ Time.at(time).utc.strftime(format) }
                    when localtime             then ->(time){ Time.at(time).iso8601 }
                    else                            ->(time){ Time.at(time).utc.iso8601 }
                    end
end

Public Instance Methods

format_nocache(time) click to toggle source
Dynamically defined in #initialize

def format(time) end

# File lib/fluent/time.rb, line 431
def format_nocache(time)
  @format_nocache.call(time)
end
format_with_subsec(time) click to toggle source
# File lib/fluent/time.rb, line 409
def format_with_subsec(time)
  if Fluent::EventTime.eq?(@tc1, time)
    return @tc1_str
  elsif Fluent::EventTime.eq?(@tc2, time)
    return @tc2_str
  else
    str = format_nocache(time)
    if @tc1 < @tc2
      @tc1 = time
      @tc1_str = str
    else
      @tc2 = time
      @tc2_str = str
    end
    return str
  end
end
format_without_subsec(time) click to toggle source
# File lib/fluent/time.rb, line 391
def format_without_subsec(time)
  if @tc1 == time
    return @tc1_str
  elsif @tc2 == time
    return @tc2_str
  else
    str = format_nocache(time)
    if @tc1 < @tc2
      @tc1 = time
      @tc1_str = str
    else
      @tc2 = time
      @tc2_str = str
    end
    return str
  end
end