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.



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# 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

Instance Method Details

#format_nocache(time) ⇒ Object

Dynamically defined in #initialize def format(time) end



431
432
433
# File 'lib/fluent/time.rb', line 431

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

#format_with_subsec(time) ⇒ Object



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# 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) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
# 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