-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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 functions for finding greatest or least time.Time supplied #65679
Comments
This sounds quite specialized, how often is this operation used outside of your specific example? |
oldest := slices.MinFunc([]time.Time{now, someTime, someTime2}, time.Time.Compare) |
Thanks @earthboundkid, I ended up here while looking for golang equivalents to |
Note that the conversion from a The documentation for each of the methods that return Unix-epoch-relative offsets says, at the time of writing:
The nanosecond-grain result does have a somewhat constrained range if you want to talk about all years where humans might have done something, but the others seem like they ought to be sufficient for most cases unless you actually need to be able to distinguish timestamps that differ only in nanoseconds. Does the ability for these functions to represent timestamps before Unix epoch make that a viable solution after all, or do you need to compare timestamps even outside of these wide ranges? 🤔 (I personally don't feel opposed to having comparison functions that are valid for all |
Proposal Details
While attempting to port over from Ruby code to Go, I ran into an issue where I needed to set a variable of type
time.Time
to the maximum of three different times. The maximum here would apply to the farthest in the future time. In Ruby this involves putting the three times in an array and calling the max method on the array.This doesn't work in Go currently because the
slices.Max
method only works on slices where of typecmp.Ordered
whichtime.Time
is not.All
time.Time
's can be converted to an int64 Unix representation which would satisfy the cmp.Ordered requirement for usingslices.Max
andslices.Min
. However, this wouldn't work for times before 0 Unix time.I'd instead purpose adding
Min
andMax
functions for thetime
package. They'd work by just using theBefore
andAfter
methods that already exist in the packageThe function signature is just like the min and max built-in functions where at least one parameter is required, that way we can just return that value if no others are passed in. The first parameter would also just get used as our base for finding the min or max of the times. This also prevents us from unintentionally returning the time.Time zero value in the Min function
The text was updated successfully, but these errors were encountered: