Class: Fluent::MixedTimeParser

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

Overview

MixedTimeParser is available when time_type is set to :mixed

Use Case 1: primary format is specified explicitly in time_format

time_type mixed
time_format %iso8601
time_format_fallbacks unixtime

Use Case 2: time_format is omitted

time_type mixed
time_format_fallbacks %iso8601, unixtime

Instance Method Summary collapse

Constructor Details

#initialize(type, format = nil, localtime = nil, timezone = nil, utc = nil, force_localtime = nil, fallbacks = []) ⇒ MixedTimeParser

to include TimeParseError



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/fluent/time.rb', line 476

def initialize(type, format = nil, localtime = nil, timezone = nil, utc = nil, force_localtime = nil, fallbacks = [])
  @parsers = []
  fallbacks.unshift(format).each do |fallback|
    next unless fallback
    case fallback
    when 'unixtime', 'float'
      @parsers << NumericTimeParser.new(fallback, localtime, timezone)
    else
      if force_localtime
        @parsers << TimeParser.new(fallback, true, nil)
      else
        localtime = localtime && (timezone.nil? && !utc)
        @parsers << TimeParser.new(fallback, localtime, timezone)
      end
    end
  end
end

Instance Method Details

#parse(value) ⇒ Object

Raises:



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/fluent/time.rb', line 494

def parse(value)
  @parsers.each do |parser|
    begin
      Float(value) if parser.class == Fluent::NumericTimeParser
    rescue
      next
    end
    begin
      return parser.parse(value)
    rescue
      # skip TimeParseError
    end
  end
  fallback_class = @parsers.collect do |parser| parser.class end.join(",")
  raise TimeParseError, "invalid time format: value = #{value}, even though fallbacks: #{fallback_class}"
end