Class: Fluent::TimeFormatter

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

Direct Known Subclasses

NumericTimeFormatter

Instance Method Summary collapse

Constructor Details

#initialize(format = nil, localtime = true, timezone = nil) ⇒ TimeFormatter

Returns a new instance of TimeFormatter.



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/fluent/time.rb', line 309

def initialize(format = nil, localtime = true, timezone = nil)
  @tc1 = 0
  @tc1_str = nil
  @tc2 = 0
  @tc2_str = 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_with_subsec))
  end

  formatter = Fluent::Timezone.formatter(timezone, format)
  @format_nocache = case
                    when formatter           then formatter
                    when format && localtime then ->(time){ Time.at(time).strftime(format) }
                    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

Instance Method Details

#format_nocache(time) ⇒ Object

Dynamically defined in #initialize def format(time) end



373
374
375
# File 'lib/fluent/time.rb', line 373

def format_nocache(time)
  @format_nocache.call(time)
end

#format_with_subsec(time) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/fluent/time.rb', line 351

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) ⇒ Object



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/fluent/time.rb', line 333

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