feat: add Max, Min, and MaxMin for datetime package

This commit is contained in:
dudaodong committed 2024-11-07 15:47:22 +08:00
1 parent e2ff83649a
commit 643edc1468
5 files changed
+194 -61

No files matched your search

+47
View File
@@ -443,3 +443,50 @@ func GenerateDatetimesBetween(start, end time.Time, layout string, interval stri
return result, nil
}
// Min returns the earliest time among the given times.
// Play: todo
func Min(t1 time.Time, times ...time.Time) time.Time {
minTime := t1
for _, t := range times {
if t.Before(minTime) {
minTime = t
}
}
return minTime
}
// Max returns the latest time among the given times.
// Play: todo
func Max(t1 time.Time, times ...time.Time) time.Time {
maxTime := t1
for _, t := range times {
if t.After(maxTime) {
maxTime = t
}
}
return maxTime
}
// MaxMin returns the latest and earliest time among the given times.
// Play: todo
func MaxMin(t1 time.Time, times ...time.Time) (maxTime time.Time, minTime time.Time) {
maxTime = t1
minTime = t1
for _, t := range times {
if t.Before(minTime) {
minTime = t
}
if t.After(maxTime) {
maxTime = t
}
}
return maxTime, minTime
}