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.



363
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
390
# File 'lib/fluent/time.rb', line 363

def initialize(format = nil, localtime = true, timezone = nil)
  # An empty cache slot is marked by a nil string, not by its key: 0 is a
  # valid timestamp, so a key can never say whether the slot is filled.
  @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



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

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

#format_with_subsec(time) ⇒ Object



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/fluent/time.rb', line 410

def format_with_subsec(time)
  if @tc1_str && Fluent::EventTime.eq?(@tc1, time)
    return @tc1_str
  elsif @tc2_str && 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



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/fluent/time.rb', line 392

def format_without_subsec(time)
  if @tc1_str && @tc1 == time
    return @tc1_str
  elsif @tc2_str && @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