Skip to content

times: limit the round-trip guarantee of between to startDt <= endDt; add anchoredBetween - #26890

Open
jasagiri wants to merge 2 commits into
nim-lang:develfrom
jasagiri:times-anchored-between
Open

jasagiri wants to merge 2 commits into
nim-lang:develfrom
jasagiri:times-anchored-between

Conversation

@jasagiri

@jasagiri jasagiri commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Refs #26825.

between(a, b) counts a negative difference from the earlier date and negates it. That keeps between(a, b) == -between(b, a) (the runnable example) but breaks the documented startDt + between(startDt, endDt) == endDt for some a > b: 64512 of the 266815 such pairs of days in 2015-2016. The two properties cannot both hold (see the issue for jiff and dateutil), so:

  1. times: limit the round-trip guarantee of between to startDt <= endDt (doc only). No behaviour change.
  2. times: add anchoredBetween (new API, can be dropped on its own; please rebase-merge if you keep both). anchoredBetween(startDt, endDt) counts from startDt in either direction and returns AnchoredInterval, a distinct TimeInterval without unary -. Months are counted first, then days in startDt.timezone, and the rest as an exact duration, so startDt + anchoredBetween(startDt, endDt) == endDt holds, also when the timezones differ.

No existing proc answers this: between (lib/pure/times.nim) is the only calendar difference in times, and it normalizes endDt < startDt to -between(endDt, startDt) by design.

Testing:

  • tests/stdlib/ttimes.nim (c refc/orc, js with and without jsbigint64): month ends and leap days in both directions, the example from std/times: between(a, b) does not round-trip when a > b, although the doc guarantees a + between(a, b) == b #26825, and times around DST changes in Europe/Stockholm and America/Belem, also against endDt.utc.
  • Outside the test file (too slow for CI): a + anchoredBetween(a, b) == b for all 534361 ordered pairs of days in 2015-2016 (UTC), and for 602176 pairs of times 30 minutes apart around DST changes in each of Europe/Stockholm, America/Belem, America/New_York and Australia/Lord_Howe; 0 failures.

🤖 Generated with Claude Code

jasagiri and others added 2 commits December 5, 2026 07:16
between counts a negative difference from the earlier date and negates it,
which keeps between(a, b) == -between(b, a) but not a + between(a, b) == b
for a > b (64512 of the 266815 such pairs of days in 2015-2016 fail).
Document the guarantee that holds.

Refs nim-lang#26825

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…irections

between(a, b) counts a negative difference from the earlier date, so
a + between(a, b) != b for some a > b (nim-lang#26825). anchoredBetween counts
from startDt in either direction and returns a distinct AnchoredInterval
without unary `-`, because negating a calendar difference does not lead
back to the anchor. It holds a + anchoredBetween(a, b) == b for all
534361 ordered pairs of days in 2015-2016 (UTC) and for 602176 pairs of
times 30 minutes apart around DST changes in each of Europe/Stockholm,
America/Belem, America/New_York and Australia/Lord_Howe, also with
endDt in UTC.

This commit only adds API and can be dropped from the PR on its own.

Refs nim-lang#26825

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Araq

Araq commented Dec 5, 2026

Copy link
Copy Markdown
Member

-1 from me. Your own code can easily use a helper modules that contains AnchoredInterval plus its operations but putting it in the stdlib makes every future user choose between AnchoredInterval and TimeInterval and to understand their differences. This is terrible, time handling is already overwhelmingly complex.

This branch has not been deployed

No deployments
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.

2 participants