Class: Fluent::TextParser::TimeParser

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

Instance Method Summary collapse

Constructor Details

#initialize(time_format) ⇒ TimeParser

Returns a new instance of TimeParser.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fluent/parser.rb', line 66

def initialize(time_format)
  @cache1_key = nil
  @cache1_time = nil
  @cache2_key = nil
  @cache2_time = nil
  @parser =
    if time_format
      Proc.new { |value| Time.strptime(value, time_format) }
    else
      Time.method(:parse)
    end
end

Instance Method Details

#parse(value) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fluent/parser.rb', line 79

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).to_i
    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