Skip to content

proposal: time: add Compare #62642

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

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

Comments

@ianlancetaylor
Copy link
Member

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 ianlancetaylor moved this to Incoming in Proposals Sep 14, 2023
@ianlancetaylor
Copy link
Member Author

Ouch, of course. Thanks.

@ianlancetaylor ianlancetaylor closed this as not planned Won't fix, can't repro, duplicate, stale Sep 14, 2023
@golang golang locked and limited conversation to collaborators Sep 13, 2024
@seankhliao seankhliao marked this as a duplicate of #71810 Feb 18, 2025
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

3 participants