Class: Fluent::TimeFormatter

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

Instance Method Summary collapse

Constructor Details

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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/fluent/mixin.rb', line 23

def initialize(format, localtime, timezone = nil)
  @tc1 = 0
  @tc1_str = nil
  @tc2 = 0
  @tc2_str = nil

  if formatter = Fluent::Timezone.formatter(timezone, format)
    define_singleton_method(:format_nocache) {|time|
      formatter.call(time)
    }
    return
  end

  if format
    if localtime
      define_singleton_method(:format_nocache) {|time|
        Time.at(time).strftime(format)
      }
    else
      define_singleton_method(:format_nocache) {|time|
        Time.at(time).utc.strftime(format)
      }
    end
  else
    if localtime
      define_singleton_method(:format_nocache) {|time|
        Time.at(time).iso8601
      }
    else
      define_singleton_method(:format_nocache) {|time|
        Time.at(time).utc.iso8601
      }
    end
  end
end

Instance Method Details

#format(time) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fluent/mixin.rb', line 59

def format(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

#format_nocache(time) ⇒ Object



77
78
79
# File 'lib/fluent/mixin.rb', line 77

def format_nocache(time)
  # will be overridden in initialize
end