v0.0.585
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m36s
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m36s
This commit is contained in:
@@ -3,6 +3,7 @@ package timeext
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -142,6 +143,38 @@ func Max(a time.Time, b time.Time) time.Time {
|
||||
}
|
||||
}
|
||||
|
||||
func Avg(v ...time.Time) time.Time {
|
||||
if len(v) == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
var sum int64
|
||||
for _, t := range v {
|
||||
sum += t.UnixNano()
|
||||
}
|
||||
|
||||
return time.Unix(0, sum/int64(len(v)))
|
||||
}
|
||||
|
||||
func Median(v ...time.Time) time.Time {
|
||||
if len(v) == 0 {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
sorted := make([]time.Time, len(v))
|
||||
copy(sorted, v)
|
||||
sort.Slice(sorted, func(i, j int) bool {
|
||||
return sorted[i].UnixNano() < sorted[j].UnixNano()
|
||||
})
|
||||
|
||||
mid := len(sorted) / 2
|
||||
if len(sorted)%2 == 0 {
|
||||
return Avg(sorted[mid-1], sorted[mid])
|
||||
} else {
|
||||
return sorted[mid]
|
||||
}
|
||||
}
|
||||
|
||||
func UnixFloatSeconds(v float64) time.Time {
|
||||
sec, dec := math.Modf(v)
|
||||
return time.Unix(int64(sec), int64(dec*(1e9)))
|
||||
|
Reference in New Issue
Block a user