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

time_truncate: simplify conversion code and tests #676

Merged
merged 1 commit into from
Jan 10, 2022
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
5 changes: 2 additions & 3 deletions lib/airbrake-ruby/time_truncate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ module Airbrake
module TimeTruncate
# Truncate +time+ to floor minute and turn it into an RFC3339 timestamp.
#
# @param [Time] time
# @param [Time, Integer, Float] time
# @return [String]
def self.utc_truncate_minutes(time)
# time does not have getutc method instead of Time on Rails 7.0.0
tm = time.respond_to?(:getutc) ? time.getutc : Time.at(time).getutc
tm = Time.at(time).getutc

Time.utc(tm.year, tm.month, tm.day, tm.hour, tm.min).to_datetime.rfc3339
end
Expand Down
33 changes: 21 additions & 12 deletions spec/time_truncate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
RSpec.describe Airbrake::TimeTruncate do
time = Time.new(2018, 1, 1, 0, 0, 20, 0)
time_with_zone = Time.new(2018, 1, 1, 0, 0, 20, '-05:00')

describe "#utc_truncate_minutes" do
it "truncates time to the floor minute and returns an RFC3339 timestamp" do
time = Time.new(2018, 1, 1, 0, 0, 20, 0)
expect(described_class.utc_truncate_minutes(time))
.to eq('2018-01-01T00:00:00+00:00')
shared_examples 'time conversion' do |t|
it "truncates the time to the floor minute and returns an RFC3339 timestamp" do
expect(described_class.utc_truncate_minutes(t))
.to eq('2018-01-01T00:00:00+00:00')
end

it "converts time with zone to UTC" do
expect(described_class.utc_truncate_minutes(time_with_zone))
.to eq('2018-01-01T05:00:00+00:00')
end
end

context "when the time argument is a Time object" do
include_examples 'time conversion', time
end

it "converts time with zone to UTC" do
time = Time.new(2018, 1, 1, 0, 0, 20, '-05:00')
expect(described_class.utc_truncate_minutes(time))
.to eq('2018-01-01T05:00:00+00:00')
context "when the time argument is a Float" do
include_examples 'time conversion', time.to_f
end

it "converts it well even if the argument is an instance of Float" do
time = Time.new(2018, 1, 1, 0, 0, 20, '-05:00').to_f
expect(described_class.utc_truncate_minutes(time))
.to eq('2018-01-01T05:00:00+00:00')
context "when the time argument is an Integer" do
include_examples 'time conversion', time.to_i
end
end
end