clamp billing-cycle resets to the month's last day - #35
Draft
dontlaugh wants to merge 3 commits into
Draft
Conversation
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
force-pushed
the
coleman/sch-7388-billing-cycle-metric-periods-start-on-the-1st-for
branch
from
September 10, 2026 14:56
311d00b to
8cdf4ea
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A subscription anchored on the 29th, 30th, or 31st gets the wrong metric period in any month with fewer days than the anchor.
GetCurrentMetricPeriodStartForCompanyBillingSubscriptionbuilt the reset withtime.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.GetNextMetricPeriodStartForSubscriptionhad 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
…Atvariants; the public functions passtime.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
TestMetricPeriodWindowin 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.gogovet finding fromtask lintis pre-existing and unrelated.