1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

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

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

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
}