Method: Fluent::TimeParser#initialize

Defined in:
lib/fluent/time.rb

#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