-
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 Time.Compare #50770
Comments
At least to me, |
Really? If you wanted to write a
or
My guess would be that most people would write it the first way. And I don't really see why it should suddenly be different if |
If I wanted to write a If I wanted to write a x := a
for !x.After(b) {
…
x = x.AddDate(0, 0, 1)
} I think that form is clearer because it emphasizes the semantics: |
In English, anyway, dates are usually compared using "before" and "after" while numbers are usually compared using "greater" or "less." It's why |
Right, but
Right, but that's just a matter of wording: "before" has the same meaning as "less". In languages with operator overloading, you would likely use My point is just that it's often natural to write |
This proposal has been added to the active column of the proposals project |
I like this proposal. Getting ordering sense right is often tricky, and the lack of these methods means that I've often needed to write out an expression in terms of how I'm thinking about it, and then change the method and add the negation. It might just be me, but I find that extra negations make it harder to think about correctness, particularly when is trying to concentrate on the details of an algorithm. However, another possibility might be to add a
This is not only fairly easy to read ( |
Considering that |
@ianlancetaylor It can matter surprisingly often in my experience - for example, a time might well be exactly equal before you've slept at all and the value is what you start with, or when you're not using |
Sure, if that's the direction that Go is moving to. It's very explicit and it would also solve my use case.
As @rogpeppe mentioned, not all |
I wasn't very enthusiastic about adding BeforeEqual and AfterEqual but adding Compare seems reasonable and fits in with the other places in the library that define a Compare. Retitling. Does anyone object to adding Time.Compare? |
Given that it's so simple to do, I just added https://go-review.googlesource.com/c/go/+/382734 to implement this. |
Based on the discussion above, this proposal seems like a likely accept. |
No change in consensus, so accepted. 🎉 |
Hi, @rogpeppe |
Change https://go.dev/cl/382734 mentions this issue: |
@hopehook thanks for the prompt. I'd forgotten about the CL entirely! I've made the appropriate changes now. |
Wouldn't it be better to add constants like |
@Delta456 We didn't do that for |
There are currently 3 methods to compare
time.Time
objects:Before()
,Equal()
andAfter()
. These are analogous to<
,==
and>
. But the equivalents of<=
and>=
are missing. I suggest addingBeforeEqual()
andAfterEqual()
to complete this.I am fully aware that these new methods are not strictly needed:
t.BeforeEqual(u)
could be written as eithert.Before(u) || t.Equal(u)
or!t.After(u)
ort.Sub(u) <= 0
. However, those three are less readable than a simple and obvioust.BeforeEqual(u)
. I have written code likewhere I felt that the comment was needed to clarify the intent.
Implementation of the proposed methods is trivial.
The text was updated successfully, but these errors were encountered: