Class: Fluent::TimeParser

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

Defined Under Namespace

Classes: TimeParseError

Instance Method Summary collapse

Constructor Details

#initialize(format = nil, localtime = true, timezone = nil) ⇒ TimeParser

Returns a new instance of TimeParser.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/fluent/time.rb', line 217

def initialize(format = nil, localtime = true, timezone = nil)
  if format.nil? && (timezone || !localtime)
    raise Fluent::ConfigError, "specifying timezone requires time format"
  end

  @cache1_key = nil
  @cache1_time = nil
  @cache2_key = nil
  @cache2_time = nil

  format_with_timezone = format && (format.include?("%z") || format.include?("%Z"))

  utc_offset = case
               when format_with_timezone then
                 nil
               when timezone then
                 Fluent::Timezone.utc_offset(timezone)
               when localtime then
                 nil
               else
                 0 # utc
               end

  strptime = format && (Strptime.new(format) rescue nil)

  @parse = case
           when format_with_timezone && strptime then ->(v){ Fluent::EventTime.from_time(strptime.exec(v)) }
           when format_with_timezone             then ->(v){ Fluent::EventTime.from_time(Time.strptime(v, format)) }
           when format == '%iso8601'             then ->(v){ Fluent::EventTime.from_time(Time.iso8601(v)) }
           when strptime then
             if utc_offset.nil?
               ->(v){ t = strptime.exec(v); Fluent::EventTime.new(t.to_i, t.nsec) }
             elsif utc_offset.respond_to?(:call)
               ->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + t.utc_offset - utc_offset.call(t), t.nsec) }
             else
               ->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + t.utc_offset - utc_offset, t.nsec) }
             end
           when format then
             if utc_offset.nil?
               ->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i, t.nsec) }
             elsif utc_offset.respond_to?(:call)
               ->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i + t.utc_offset - utc_offset.call(t), t.nsec) }
             else
               ->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i + t.utc_offset - utc_offset, t.nsec) }
             end
           else ->(v){ Fluent::EventTime.parse(v) }
           end
end

Instance Method Details

#parse(value) ⇒ Object Also known as: call

TODO: new cache mechanism using format string



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/fluent/time.rb', line 267

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

  if @cache1_key == value
    return @cache1_time
  elsif @cache2_key == value
    return @cache2_time
  else
    begin
      time = @parse.call(value)
    rescue => e
      raise TimeParseError, "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