Class: Fluent::Plugin::Parser::TimeParser

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

Direct Known Subclasses

Compat::TextParser::TimeParser

Instance Method Summary collapse

Constructor Details

#initialize(time_format) ⇒ TimeParser

Returns a new instance of TimeParser.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/fluent/plugin/parser.rb', line 56

def initialize(time_format)
  @cache1_key = nil
  @cache1_time = nil
  @cache2_key = nil
  @cache2_time = nil
  @parser =
    if time_format
      begin
        strptime = Strptime.new(time_format)
        Proc.new { |value| Fluent::EventTime.from_time(strptime.exec(value)) }
      rescue
        Proc.new { |value| Fluent::EventTime.from_time(Time.strptime(value, time_format)) }
      end
    else
      Proc.new { |value| Fluent::EventTime.parse(value) }
    end
end

Instance Method Details

#parse(value) ⇒ Object

TODO: new cache mechanism using format string



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/fluent/plugin/parser.rb', line 75

def parse(value)
  unless value.is_a?(String)
    raise ParserError, "value must be string: #{value}"
  end

  if @cache1_key == value
    return @cache1_time
  elsif @cache2_key == value
    return @cache2_time
  else
    begin
      time = @parser.call(value)
    rescue => e
      raise ParserError, "invalid time format: value = #{value}, error_class = #{e.class.name}, error = #{e.message}"
    end
    @cache1_key = @cache2_key
    @cache1_time = @cache2_time
    @cache2_key = value
    @cache2_time = time
    return time
  end
end