v0.0.585
All checks were successful
Build Docker and Deploy / Run goext test-suite (push) Successful in 2m36s

This commit is contained in:
2025-07-04 11:46:00 +02:00
parent 37e52595a2
commit 09932046f8
5 changed files with 208 additions and 3 deletions

View File

@@ -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)))