Skip to content

clamp billing-cycle resets to the month's last day - #35

Draft
dontlaugh wants to merge 3 commits into
mainfrom
coleman/sch-7388-billing-cycle-metric-periods-start-on-the-1st-for
Draft

clamp billing-cycle resets to the month's last day#35
dontlaugh wants to merge 3 commits into
mainfrom
coleman/sch-7388-billing-cycle-metric-periods-start-on-the-1st-for

Conversation

@dontlaugh

@dontlaugh dontlaugh commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

A subscription anchored on the 29th, 30th, or 31st gets the wrong metric period in any month with fewer days than the anchor. GetCurrentMetricPeriodStartForCompanyBillingSubscription built the reset with time.Date(now.Year(), now.Month(), periodStart.Day(), …), so "September 31" normalized to October 1, landed in the future, and the code stepped back to September 1 instead of August 31; usage from the tail of the previous cycle then counted against the new one. GetNextMetricPeriodStartForSubscription had the identical construction.

Both now place the anniversary inside the target month, clamped to its last day, and move to the adjacent month by recomputing the anniversary there rather than with AddDate, so an anchor on the 31st resets on the 30th in a 30-day month and on the 28th or 29th in February.

The two functions take the evaluation instant through unexported …At variants; the public functions pass time.Now(). That lets the tests drive the clock: a sweep of every anchor day across two years checks both functions day by day against an independent oracle (latest reset at or before now, earliest after, one calendar month apart, day clamped to the month), every anchor day in every month of leap, non-leap, and century years agrees with the oracle, the reset instant and the nanosecond before it fall on opposite sides, and the period-end cap, future starts, and the no-subscription fallback are pinned. The clock-based tests that run against the real date stay too.

Surfaced by TestMetricPeriodWindow in schematic-api, which anchored a subscription ten days before now and failed for all of 2026-09-10 UTC (since re-anchored on the 10th in schematic-api #8318, which unblocks CI but leaves this bug in place).

The version.go govet finding from task lint is pre-existing and unrelated.

A subscription anchored on the 29th, 30th, or 31st got the wrong metric period in shorter months: time.Date normalized "September 31" into October 1, which was in the future, so the current reset stepped back to September 1 instead of August 31. The next reset was misplaced the same way. Both functions now place the anniversary within the target month, clamped to its last day, and step to the adjacent month by recomputing the anniversary there rather than by AddDate.
The two period functions now take the evaluation instant through unexported variants so their month arithmetic can be tested on any date; the public functions pass time.Now. Tests sweep every anchor day across two years against an independent oracle, check every anchor day in every month of leap, non-leap, and century years, pin the reset instant and the nanosecond before it, and cover the period-end cap, future starts, and the no-subscription fallback.
@dontlaugh
dontlaugh force-pushed the coleman/sch-7388-billing-cycle-metric-periods-start-on-the-1st-for branch from 311d00b to 8cdf4ea Compare September 10, 2026 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant