From 2a1e8ab3d37538d1109819475ca8b3969aafd529 Mon Sep 17 00:00:00 2001 From: Steve Gray Date: Mon, 4 Feb 2019 16:30:55 -0600 Subject: [PATCH] Maintain UTC offset when truncating Previously, the time would be converted to UTC without properly adjusting for the given time's time zone. --- lib/airbrake-ruby/time_truncate.rb | 8 ++------ spec/time_truncate_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/airbrake-ruby/time_truncate.rb b/lib/airbrake-ruby/time_truncate.rb index 4005e5bb..8bb57e81 100644 --- a/lib/airbrake-ruby/time_truncate.rb +++ b/lib/airbrake-ruby/time_truncate.rb @@ -9,13 +9,9 @@ module TimeTruncate # @param [Time] time # @return [String] def self.utc_truncate_minutes(time) - time_array = time.to_a - time_array[0] = 0 - tm = Time.utc(*time_array) + tm = time.getutc - Time.new( - tm.year, tm.month, tm.day, tm.hour, tm.min, 0, tm.utc_offset || 0 - ).to_datetime.rfc3339 + Time.utc(tm.year, tm.month, tm.day, tm.hour, tm.min).to_datetime.rfc3339 end end end diff --git a/spec/time_truncate_spec.rb b/spec/time_truncate_spec.rb index 433f0b8a..be388cdc 100644 --- a/spec/time_truncate_spec.rb +++ b/spec/time_truncate_spec.rb @@ -6,5 +6,10 @@ time = Time.new(2018, 1, 1, 0, 0, 20, 0) expect(subject.utc_truncate_minutes(time)).to eq('2018-01-01T00:00:00+00:00') end + + it "converts time with zone to UTC" do + time = Time.new(2018, 1, 1, 0, 0, 20, '-05:00') + expect(subject.utc_truncate_minutes(time)).to eq('2018-01-01T05:00:00+00:00') + end end end