-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
time: add Duration.Abs #51414
Comments
I think it would be better to provide an "absolute subtraction" or "difference" method instead (if anything): // AbsSub returns abs(t-u). Always non-negative. If the result is too large it returns the maximum value.
// FIXME: naming
func (t Time) AbsSub(u Time) Duration This allows people to do the delta comparison if they like, or something else. For my use cases I find:
I agree comparison with the zero time increases the likelihood of getting this wrong (the bug/risk is non-obvious). If Observation: If |
That’s a good suggestion. It could be called AbsDiff or plain old Diff. |
It sounds like today you would write:
and the suggestion is to instead be able to write:
? I'm not sure I would expect Diff to be absolute value of Sub, but maybe I'm unfamiliar with usage elsewhere? |
The problem is that it is very easy to write this buggy code instead:
I'm fine with the name |
bit of a mouthful but |
This proposal has been added to the active column of the proposals project |
It may be easy to write that buggy code but it seems easier to write the correct code ( Hacker's Delight refers to max(x-y, 0) as "difference or zero", suggesting that x-y (not |x-y|) is a typical meaning of "difference". Perhaps we should instead add Duration.Abs, defined that time.Duration(math.MinInt64).Abs() == math.MaxInt64. |
I don't think That is: it seems to me that this proposal would address one specific failure mode, but leave many of the other |
Methods In retrospect, if Duration were |
If the current time.Duration turns out to have many infelicities, then a new, better designed package could perhaos help? |
Even if there is a time2go package at some point in the future, it would be good to make improvements to the existing time package for projects stuck on old time. Ideas proposed in this issue:
Do people have a strong sense that we should do all of these? Some of these? None of these? My feeling is that Absolute and Saturated are improvements to safety, so they should definitely happen, and then Within is convenient so it should maybe happen, and AbsSub is sort of redundant if Duration.Absolute exists, so it can be left off. |
time is a very low-level package. We don't want to dump all possible APIs into it. If people feel strongly about |
Floating point numbers stay saturated once they hit infinity, but Duration will wrap, so IsInf seems less useful unless we also added ScaleBy, DivideBy, Add, and Sub from #20757. Or add saturating arithmetic operators to int (don't know the issue number for that, but I'm sure it exists). Abs is probably enough for now, and the other safety methods can be revisited later. |
Based on the discussion above, this proposal seems like a likely accept. |
Change https://go.dev/cl/393515 mentions this issue: |
No change in consensus, so accepted. 🎉 |
#50770 adds Time.Compare, which is good, however, as noted in the issue, because times have nanosecond precision, it is unusual for times to be exactly equal to each other unless they've been truncated somehow. One might think, oh, no big deal, just do | t1 - t2 | < delta. It isn't such a big deal, but there is a subtle bug here (which bit me, naturally). time.Durations are measured as nanoseconds and can only measure 260 years or so. If t1 is the zero time and t2 is not, t1.Sub(t2) produces a Duration that cannot be converted from negative to positive because it is math.MinInt64.
See playground:
Anyhow, getting this right seems subtle enough to be worth adding a helper method to time.Time.
The text was updated successfully, but these errors were encountered: