github.com/sagernet/quic-go@v0.43.1-beta.1/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  // MinTime returns the earlier time
    39  func MinTime(a, b time.Time) time.Time {
    40  	if a.After(b) {
    41  		return b
    42  	}
    43  	return a
    44  }
    45  
    46  // MaxTime returns the later time
    47  func MaxTime(a, b time.Time) time.Time {
    48  	if a.After(b) {
    49  		return a
    50  	}
    51  	return b
    52  }