github.com/MerlinKodo/quic-go@v0.39.2/internal/utils/minmax.go (about) 1 package utils 2 3 import ( 4 "math" 5 "time" 6 7 "golang.org/x/exp/constraints" 8 ) 9 10 // InfDuration is a duration of infinite length 11 const InfDuration = time.Duration(math.MaxInt64) 12 13 func Max[T constraints.Ordered](a, b T) T { 14 if a < b { 15 return b 16 } 17 return a 18 } 19 20 func Min[T constraints.Ordered](a, b T) T { 21 if a < b { 22 return a 23 } 24 return b 25 } 26 27 // MinNonZeroDuration return the minimum duration that's not zero. 28 func MinNonZeroDuration(a, b time.Duration) time.Duration { 29 if a == 0 { 30 return b 31 } 32 if b == 0 { 33 return a 34 } 35 return Min(a, b) 36 } 37 38 // AbsDuration returns the absolute value of a time duration 39 func AbsDuration(d time.Duration) time.Duration { 40 if d >= 0 { 41 return d 42 } 43 return -d 44 } 45 46 // MinTime returns the earlier time 47 func MinTime(a, b time.Time) time.Time { 48 if a.After(b) { 49 return b 50 } 51 return a 52 } 53 54 // MinNonZeroTime returns the earlist time that is not time.Time{} 55 // If both a and b are time.Time{}, it returns time.Time{} 56 func MinNonZeroTime(a, b time.Time) time.Time { 57 if a.IsZero() { 58 return b 59 } 60 if b.IsZero() { 61 return a 62 } 63 return MinTime(a, b) 64 } 65 66 // MaxTime returns the later time 67 func MaxTime(a, b time.Time) time.Time { 68 if a.After(b) { 69 return a 70 } 71 return b 72 }