-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: time: add time.Same, time.Past and time.Future const or enum for time.Compare #57177
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
Comments
The -1, 0, +1 convention is relatively common:
I don't think we want to set the precedence that each of these should be accompanied by a constant. |
For me, the problem is remembering how they're being compared. Example: if t1.Compare(t2) == time.Future {
// is it t1 or t2 that's in the future?
} |
The mnemonic I use to remember t1.Compare(t2) < 0 // is equivalent to t1 < t2
t1.Compare(t2) > 0 // is equivalent to t1 > t2 Essentially, you replace the Similarly: t1.Compare(t2) <= 0 // is equivalent to t1 <= t2
t1.Compare(t2) >= 0 // is equivalent to t1 >= t2
t1.Compare(t2) == 0 // is equivalent to t1 == t2 |
Oh, I see. I think the current way matches the semantics of |
From commit, I think we should be using an enum or const for the return type of
time.Compare
instead of hardcoded values. It makes the code easier to understand and maintain. This can be seen below:Example code:
The text was updated successfully, but these errors were encountered: