class Fluent::TimeParser
Public Class Methods
new(format = nil, localtime = true, timezone = nil)
click to toggle source
# 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
Public Instance Methods
parse(value)
click to toggle source
TODO: new cache mechanism using format string
# 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
Also aliased as: call