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

Generate correct epoch timestamp even after switching Daylight Saving Time #3524

Merged
merged 4 commits into from
Oct 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion fluentd.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency("sigdump", ["~> 0.2.2"])
gem.add_runtime_dependency("tzinfo", [">= 1.0", "< 3.0"])
gem.add_runtime_dependency("tzinfo-data", ["~> 1.0"])
gem.add_runtime_dependency("strptime", [">= 0.2.2", "< 1.0.0"])
gem.add_runtime_dependency("strptime", [">= 0.2.4", "< 1.0.0"])
gem.add_runtime_dependency("webrick", [">= 1.4.2", "< 1.8.0"])

# build gem for a certain platform. see also Rakefile
Expand Down
41 changes: 21 additions & 20 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,16 @@ def initialize(format = nil, localtime = true, timezone = nil)

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

# unixtime_in_expected_tz = unixtime_in_localtime + offset_diff
offset_diff = case
when format_with_timezone then nil
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
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)

Expand All @@ -247,16 +244,20 @@ def initialize(format = nil, localtime = true, timezone = nil)
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 offset_diff.respond_to?(:call)
->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + offset_diff.call(t), t.nsec) }
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 + offset_diff, t.nsec) }
->(v) { t = strptime.exec(v); Fluent::EventTime.new(t.to_i + t.utc_offset - utc_offset, 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) }
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 + offset_diff, t.nsec) }
->(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
Expand Down
22 changes: 22 additions & 0 deletions test/test_time_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,26 @@ class DummyForTimeParser
assert_equal_event_time(time, parser.parse('2021-01-01T12:00:00+0900'))
end
end

# https://github.com/fluent/fluentd/issues/3195
test 'change timezone without zone specifier in a format' do
expected = 1607457600 # 2020-12-08T20:00:00Z
time1 = time2 = nil

with_timezone("UTC-05") do # EST
i = DummyForTimeParser.new
i.configure(config_element('parse', '', {'time_type' => 'string',
'time_format' => '%Y-%m-%dT%H:%M:%SZ',
'utc' => true}))
parser = i.time_parser_create

time1 = parser.parse('2020-12-08T20:00:00Z').to_i
time2 = with_timezone("UTC-04") do # EDT
# to avoid using cache, increment 1 sec
parser.parse('2020-12-08T20:00:01Z').to_i
end
end

assert_equal([expected, expected + 1], [time1, time2])
end
end