Skip to content
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

proposal: time: add time.Same, time.Past and time.Future const or enum for time.Compare #57177

Closed
Delta456 opened this issue Dec 8, 2022 · 4 comments

Comments

@Delta456
Copy link

Delta456 commented Dec 8, 2022

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:

// src/time/time.go
package time

const (
  Same = 0
  Past = +1
  Furue = -1
)

// ...

Example code:

package main

import (
	"fmt"
	"time"
)

func main() {
	var (
		t1 = time.Now()
		t2 = t1.Add(time.Hour)
	)
	switch t1.Compare(t2) {
	case time.Same:
		fmt.Println("same")
	case time.Past:
		fmt.Println("past")
	case time.Future:
		fmt.Println("future")
	}
}
@seankhliao seankhliao changed the title time: add time.Same, time.Past and time.Future const or enum for time.Compare proposal: time: add time.Same, time.Past and time.Future const or enum for time.Compare Dec 8, 2022
@gopherbot gopherbot added this to the Proposal milestone Dec 8, 2022
@dsnet
Copy link
Member

dsnet commented Dec 8, 2022

The -1, 0, +1 convention is relatively common:

  • bytes.Compare
  • subtle.ConstantTimeCompare
  • netip.Addr.Compare

I don't think we want to set the precedence that each of these should be accompanied by a constant.

@icholy
Copy link

icholy commented Dec 8, 2022

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?
}

@dsnet
Copy link
Member

dsnet commented Dec 8, 2022

The mnemonic I use to remember Compare like operations is that:

t1.Compare(t2) < 0 // is equivalent to t1 < t2
t1.Compare(t2) > 0 // is equivalent to t1 > t2

Essentially, you replace the Compare with the < or > operator that is just to the right of it.

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

@Delta456
Copy link
Author

Delta456 commented Dec 9, 2022

The mnemonic I use to remember Compare like operations is that:

t1.Compare(t2) < 0 // is equivalent to t1 < t2
t1.Compare(t2) > 0 // is equivalent to t1 > t2

Essentially, you replace the Compare with the < or > operator that is just to the right of it.

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 bytes.Compare, subtle.ConstantTimeCompare, netip.Addr.Compare so this proposal can be closed.

@Delta456 Delta456 closed this as completed Dec 9, 2022
@golang golang locked and limited conversation to collaborators Dec 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants