Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error if timezone with name is used in parser #2421

Merged
merged 4 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,13 @@ def initialize(format = nil, localtime = true, timezone = nil)
# unixtime_in_expected_tz = unixtime_in_localtime + offset_diff
offset_diff = case
when format_with_timezone then nil
when timezone then Time.now.localtime.utc_offset - Time.zone_offset(timezone)
when timezone then
offset = Fluent::Timezone.utc_offset(timezone)
if offset.respond_to?(:call)
->(t) { Time.now.localtime.utc_offset - offset.call(t) }
else
Time.now.localtime.utc_offset - offset
end
when localtime then 0
else Time.now.localtime.utc_offset # utc
end
Expand All @@ -214,8 +220,18 @@ def initialize(format = nil, localtime = true, timezone = nil)
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 ->(v){ t = strptime.exec(v); Fluent::EventTime.new(t.to_i + offset_diff, t.nsec) }
when format then ->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i + offset_diff, t.nsec) }
when strptime then
if offset_diff.respond_to?(:call)
->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + offset_diff.call(t), t.nsec) }
else
->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + offset_diff, t.nsec) }
end
when format then
if offset_diff.respond_to?(:call)
->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i + offset_diff.call(t), t.nsec) }
else
->(v){ t = Time.strptime(v, format); Fluent::EventTime.new(t.to_i + offset_diff, t.nsec) }
end
else ->(v){ Fluent::EventTime.parse(v) }
end
end
Expand Down
8 changes: 8 additions & 0 deletions test/test_time_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ def test_parse_string_with_expected_timezone
assert_equal_event_time(time, event_time("2016-09-02 18:42:31.123456789 -07:00", format: '%Y-%m-%d %H:%M:%S.%N %z'))
end

def test_parse_time_with_expected_timezone_name
time = with_timezone("UTC-09") do
parser = Fluent::TimeParser.new("%Y-%m-%d %H:%M:%S.%N", nil, "Europe/Zurich")
parser.parse("2016-12-02 18:42:31.123456789")
end
assert_equal_event_time(time, event_time("2016-12-02 18:42:31.123456789 +01:00", format: '%Y-%m-%d %H:%M:%S.%N %z'))
end

sub_test_case 'TimeMixin::Parser' do
class DummyForTimeParser
include Fluent::Configurable
Expand Down