Skip to content

Exact ISO 8601 calendar arithmetic for Y/M durations#62

Open
akselinurmio wants to merge 2 commits into
sosodev:mainfrom
akselinurmio:unfuzzy
Open

Exact ISO 8601 calendar arithmetic for Y/M durations#62
akselinurmio wants to merge 2 commits into
sosodev:mainfrom
akselinurmio:unfuzzy

Conversation

@akselinurmio
Copy link
Copy Markdown

Why

FromTimeDuration and ToTimeDuration approximated months as 365/12 days and years as 365 days. This violates ISO 8601, which defines calendar durations relative to a reference point (a month is not a fixed number of seconds).

What

  • FromTimeDuration now decomposes into exact units only (W/D/H/M/S). Years and months are never inferred from a nanosecond count.
  • ToTimeDuration deprecated for use with Y/M values.
  • New Shift(ref time.Time) time.Time applies a duration using real calendar arithmetic with ISO 8601 day-preservation clamping (Jan 31 + 1M = Feb 28).
  • New ToTimeDurationFrom(ref time.Time) time.Duration returns the exact elapsed nanoseconds via Shift.

Compatibility

No breaking changes. All existing signatures preserved.

Remove fuzzy year/month decomposition from FromTimeDuration; it now
decomposes time.Duration into Weeks/Days/Hours/Minutes/Seconds only,
using integer nanosecond division.

Also update Format and ToTimeDuration doc comments; deprecate
ToTimeDuration for use with Years/Months.
Shift(t time.Time) applies a Duration to a reference time using
time.AddDate for Years/Months, with ISO 8601 day-preservation clamping
(e.g. Jan 31 + 1M = Feb 28). Fractional Y/M are truncated; fractional
W/D carry into the nanosecond offset.

ToTimeDurationFrom(ref time.Time) returns Shift(ref).Sub(ref), giving
an exact nanosecond count for all units including Years and Months.
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