diff --git a/lib/fluent/plugin_helper/compat_parameters.rb b/lib/fluent/plugin_helper/compat_parameters.rb index 567760b315..bd2819f64b 100644 --- a/lib/fluent/plugin_helper/compat_parameters.rb +++ b/lib/fluent/plugin_helper/compat_parameters.rb @@ -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']) @@ -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') diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index 3578ed0d2c..5c56c953e1 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -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'])) @@ -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 diff --git a/test/plugin/test_out_stdout.rb b/test/plugin/test_out_stdout.rb index cbb028bcdc..4b89e114b2 100644 --- a/test/plugin/test_out_stdout.rb +++ b/test/plugin/test_out_stdout.rb @@ -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 @@ -167,4 +184,3 @@ def capture_log(&block) $log = tmp end end -