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

Accept localtime xor utc #2720

Merged
merged 1 commit into from
Dec 10, 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
12 changes: 11 additions & 1 deletion lib/fluent/plugin_helper/compat_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ def compat_parameters_inject(conf)
hash['time_type'] = 'unixtime'
end
if conf.has_key?('localtime') || conf.has_key?('utc')
if conf.has_key?('localtime') && conf.has_key?('utc')
utc = to_bool(conf['utc'])
localtime = to_bool(conf['localtime'])
if conf.has_key?('localtime') && conf.has_key?('utc') && !(localtime ^ utc)
raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
elsif conf.has_key?('localtime')
hash['localtime'] = Fluent::Config.bool_value(conf['localtime'])
Expand All @@ -217,6 +219,14 @@ def compat_parameters_inject(conf)
conf
end

def to_bool(v)
if v.is_a?(FalseClass) || v == 'false' || v.nil?
false
else
true
end
end

def compat_parameters_extract(conf)
return unless conf.elements('extract').empty?
return if EXTRACT_PARAMS.keys.all?{|k| !conf.has_key?(k) } && !conf.has_key?('format')
Expand Down
8 changes: 5 additions & 3 deletions lib/fluent/time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ module TimeParameters

def configure(conf)
if conf.has_key?('localtime') || conf.has_key?('utc')
if conf.has_key?('localtime') && conf.has_key?('utc')
raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
elsif conf.has_key?('localtime')
if conf.has_key?('localtime')
conf['localtime'] = Fluent::Config.bool_value(conf['localtime'])
elsif conf.has_key?('utc')
conf['localtime'] = !(Fluent::Config.bool_value(conf['utc']))
Expand All @@ -167,6 +165,10 @@ def configure(conf)

super

if conf.has_key?('localtime') && conf.has_key?('utc') && !(@localtime ^ @utc)
raise Fluent::ConfigError, "both of utc and localtime are specified, use only one of them"
end

Fluent::Timezone.validate!(@timezone) if @timezone
end
end
Expand Down
18 changes: 17 additions & 1 deletion test/plugin/test_out_stdout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,23 @@ def create_driver(conf = CONFIG)
end
end

data(
'utc and !localtime' => "utc true\nlocaltime false",
'!utc and localtime' => "utc false\nlocaltime true")
test 'configure with localtime and utc' do |c|
assert_nothing_raised do
create_driver(CONFIG + c)
end
end

data('utc and localtime' => "utc true\nlocaltime true",
'!utc and !localtime' => "utc false\nlocaltime false")
test 'configure with localtime and utc' do |c|
assert_raise(Fluent::ConfigError.new('both of utc and localtime are specified, use only one of them')) do
create_driver(CONFIG + c)
end
end

# Capture the log output of the block given
def capture_log(&block)
tmp = $log
Expand All @@ -167,4 +184,3 @@ def capture_log(&block)
$log = tmp
end
end