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 Compare #62642

Closed
ianlancetaylor opened this issue Sep 14, 2023 · 2 comments
Closed

proposal: time: add Compare #62642

ianlancetaylor opened this issue Sep 14, 2023 · 2 comments
Labels
Milestone

Comments

@ianlancetaylor
Copy link
Contributor

We have a method time.Time.Compare, but we don't have a plain time.Compare function. The latter would be useful as an argument to functions like slices.SortFunc or slices.BinarySearchFunc. I propose

// Compare returns an integer comparing two times.
// If a is before b, it returns -1;
// if a is after b, it returns +1; if they're the same, it returns 0.
func Compare(a, b time.Time) int {
	return a.Compare(b)
}
@gopherbot gopherbot added this to the Proposal milestone Sep 14, 2023
@mateusz834
Copy link
Member

mateusz834 commented Sep 14, 2023

@ianlancetaylor

No need for a new function, time.Time.Compare can be used as:

package main

import (
	"fmt"
	"slices"
	"time"
)

func main() {
	times := []time.Time{time.Now(), time.Now(), time.Time{}}
	slices.SortFunc(times, time.Time.Compare)
	fmt.Println(times)
}

https://go.dev/play/p/IrQkNDijzYt

@ianlancetaylor
Copy link
Contributor Author

Ouch, of course. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants