You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
my_time=[someTime,someTime2,DateTime.now].max
This doesn't work in Go currently because the slices.Max method only works on slices where of type cmp.Ordered which time.Time is not.
All time.Time's can be converted to an int64 Unix representation which would satisfy the cmp.Ordered requirement for using slices.Max and slices.Min. However, this wouldn't work for times before 0 Unix time.
I'd instead purpose adding Min and Max functions for the time package. They'd work by just using the Before and After methods that already exist in the package
The 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:
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: