Exact ISO 8601 calendar arithmetic for Y/M durations#62
Open
akselinurmio wants to merge 2 commits into
Open
Conversation
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.
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.
Why
FromTimeDurationandToTimeDurationapproximated 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
FromTimeDurationnow decomposes into exact units only (W/D/H/M/S). Years and months are never inferred from a nanosecond count.ToTimeDurationdeprecated for use with Y/M values.Shift(ref time.Time) time.Timeapplies a duration using real calendar arithmetic with ISO 8601 day-preservation clamping (Jan 31 + 1M = Feb 28).ToTimeDurationFrom(ref time.Time) time.Durationreturns the exact elapsed nanoseconds viaShift.Compatibility
No breaking changes. All existing signatures preserved.