diff --git a/lib/fluent/time.rb b/lib/fluent/time.rb index 691db93e9a..c512f5aef8 100644 --- a/lib/fluent/time.rb +++ b/lib/fluent/time.rb @@ -457,7 +457,7 @@ def format_float(time) if time.is_a?(Fluent::EventTime) || time.is_a?(Time) # 10.015 secs for 10_000_000 times call on MacBookAir nsec_s = time.nsec.to_s - nsec_s = '0' * (9 - nsec_s.size) if nsec_s.size < 9 + nsec_s = nsec_s.rjust(9, '0') if nsec_s.size < 9 "#{time.sec}.#{nsec_s}" else # integer (or float?) time.to_f.to_s diff --git a/test/test_time_formatter.rb b/test/test_time_formatter.rb index ec808b959f..98a957c901 100644 --- a/test/test_time_formatter.rb +++ b/test/test_time_formatter.rb @@ -360,4 +360,15 @@ class DummyForTimeFormatter time = event_time("2016-10-03 20:08:30.123456789 +0100", format: '%Y-%m-%d %H:%M:%S.%N %z') assert_equal "#{time.sec}.#{time.nsec}", fmt.format(time) end + + data('under 0.1 second' => [12345678, '1700000000.012345678'], + '5 microseconds' => [5000, '1700000000.000005000'], + '1 nanosecond' => [1, '1700000000.000000001'], + 'zero' => [0, '1700000000.000000000']) + test 'NumericTimeFormatter pads nanoseconds with leading zeros when time_type float specified' do |(nsec, expected)| + i = DummyForTimeFormatter.new + i.configure(config_element('format', '', {'time_type' => 'float'})) + fmt = i.time_formatter_create + assert_equal expected, fmt.format(Fluent::EventTime.new(1700000000, nsec)) + end end