Skip to content

Commit

Permalink
fix: Date prorata with daylight saving time (2/2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Nov 6, 2023
1 parent c92e1a2 commit ecceafa
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,11 @@ def period_duration
# we want to bill the persisted metrics at prorata of the full period duration.
# ie: the number of day of the terminated period divided by number of days without termination
def persisted_pro_rata
((to_datetime.to_time - from_datetime.to_time) / 1.day).ceil.fdiv(period_duration)
Utils::DatetimeService.date_diff_with_timezone(
from_datetime,
to_datetime,
subscription.customer.applicable_timezone,
).fdiv(period_duration)
end

def persisted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ def period_duration
# we want to bill the persisted metrics at prorata of the full period duration.
# ie: the number of day of the terminated period divided by number of days without termination
def persisted_pro_rata
((to_datetime.to_time - from_datetime.to_time) / 1.day).ceil.fdiv(period_duration)
Utils::DatetimeService.date_diff_with_timezone(
from_datetime,
to_datetime,
subscription.customer.applicable_timezone,
).fdiv(period_duration)
end

def per_event_aggregation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
end

let(:subscription_at) { DateTime.parse('2022-06-09') }
let(:subscription_at) { Time.zone.parse('2022-06-09') }
let(:started_at) { subscription_at }
let(:organization) { subscription.organization }
let(:customer) { subscription.customer }
Expand All @@ -42,8 +42,8 @@
)
end

let(:from_datetime) { DateTime.parse('2022-07-09 00:00:00 UTC') }
let(:to_datetime) { DateTime.parse('2022-08-08 23:59:59 UTC') }
let(:from_datetime) { Time.zone.parse('2022-07-09 00:00:00 UTC') }
let(:to_datetime) { Time.zone.parse('2022-08-08 23:59:59 UTC') }

let(:added_at) { from_datetime - 1.month }
let(:removed_at) { nil }
Expand Down Expand Up @@ -79,7 +79,7 @@
status: :terminated,
)
end
let(:to_datetime) { DateTime.parse('2022-07-24 23:59:59') }
let(:to_datetime) { Time.zone.parse('2022-07-24 23:59:59') }

it 'returns the prorata of the full duration' do
expect(result.aggregation).to eq(16.fdiv(31).ceil(5))
Expand All @@ -97,7 +97,7 @@
status: :terminated,
)
end
let(:to_datetime) { DateTime.parse('2022-07-24 23:59:59') }
let(:to_datetime) { Time.zone.parse('2022-07-24 23:59:59') }

before do
create(
Expand All @@ -115,7 +115,7 @@
end

context 'when subscription was started in the period' do
let(:started_at) { DateTime.parse('2022-08-01') }
let(:started_at) { Time.zone.parse('2022-08-01') }
let(:from_datetime) { started_at }

it 'returns the prorata of the full duration' do
Expand Down Expand Up @@ -213,7 +213,7 @@
status: :terminated,
)
end
let(:to_datetime) { DateTime.parse('2022-07-24 23:59:59') }
let(:to_datetime) { Time.zone.parse('2022-07-24 23:59:59') }

it 'returns the detail the persisted metrics' do
aggregate_failures do
Expand All @@ -240,7 +240,7 @@
status: :terminated,
)
end
let(:to_datetime) { DateTime.parse('2022-07-24 23:59:59') }
let(:to_datetime) { Time.zone.parse('2022-07-24 23:59:59') }

before do
create(
Expand Down Expand Up @@ -295,7 +295,7 @@
end

context 'when subscription was started in the period' do
let(:started_at) { DateTime.parse('2022-08-01') }
let(:started_at) { Time.zone.parse('2022-08-01') }
let(:from_datetime) { started_at }

it 'returns the detail the persisted metrics' do
Expand Down

0 comments on commit ecceafa

Please sign in to comment.