Skip to content

Commit

Permalink
fix: Fix a small edge case with datetime computation (#1713)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-pochet authored Feb 22, 2024
1 parent 84a7a8b commit 9a1839d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/services/utils/datetime_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def self.date_diff_with_timezone(from_datetime, to_datetime, timezone)

to = to_datetime
to = Time.zone.parse(to.to_s) unless to.is_a?(ActiveSupport::TimeWithZone)
to += 1.second if to == to.beginning_of_day # To make sure we do not miss a day

from_offset = from.in_time_zone(timezone).utc_offset
to_offset = to.in_time_zone(timezone).utc_offset
Expand Down
4 changes: 2 additions & 2 deletions spec/scenarios/charge_models/prorated_graduated_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@
aggregate_failures do
expect(subscription.reload).to be_terminated
expect(subscription.reload.invoices.count).to eq(1)
expect(invoice.total_amount_cents).to eq(226)
expect(invoice.total_amount_cents).to eq(258)
expect(invoice.issuing_date.iso8601).to eq('2023-12-07')
end
end
Expand Down Expand Up @@ -1050,7 +1050,7 @@
aggregate_failures do
expect(subscription.reload).to be_terminated
expect(subscription.reload.invoices.count).to eq(1)
expect(invoice.total_amount_cents).to eq(4_145)
expect(invoice.total_amount_cents).to eq(4_161)
expect(invoice.issuing_date.iso8601).to eq('2023-12-07')
end
end
Expand Down
10 changes: 10 additions & 0 deletions spec/services/utils/datetime_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@
expect(result).to eq(31)
end
end

context 'with to date is the beginning of the day' do
let(:from_datetime) { Time.zone.parse('2023-12-01T00:00:00') }
let(:to_datetime) { Time.zone.parse('2023-12-07T00:00:00') }
let(:timezone) { 'UTC' }

it 'ensures it counts the full days' do
expect(result).to eq(7)
end
end
end

describe '.period_total_length_in_days' do
Expand Down

0 comments on commit 9a1839d

Please sign in to comment.