mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-23 11:53:08 +00:00
42 lines
765 B
Go
42 lines
765 B
Go
package types
|
|
|
|
import (
|
|
"sort"
|
|
"time"
|
|
)
|
|
|
|
type Durations []time.Duration
|
|
|
|
func (d Durations) Sort(ascending ...bool) {
|
|
// If ascending is provided and is false, sort in descending order
|
|
if len(ascending) > 0 && ascending[0] == false {
|
|
sort.Slice(d, func(i, j int) bool {
|
|
return d[i] > d[j]
|
|
})
|
|
} else { // Otherwise, sort in ascending order
|
|
sort.Slice(d, func(i, j int) bool {
|
|
return d[i] < d[j]
|
|
})
|
|
}
|
|
}
|
|
|
|
func (d Durations) First() *time.Duration {
|
|
return &d[0]
|
|
}
|
|
|
|
func (d Durations) Last() *time.Duration {
|
|
return &d[len(d)-1]
|
|
}
|
|
|
|
func (d Durations) Sum() time.Duration {
|
|
sum := time.Duration(0)
|
|
for _, duration := range d {
|
|
sum += duration
|
|
}
|
|
return sum
|
|
}
|
|
|
|
func (d Durations) Avg() time.Duration {
|
|
return d.Sum() / time.Duration(len(d))
|
|
}
|