1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

refactor: introduce concept of durations (resolve #261)

This commit is contained in:
Ferdinand Mütsch
2021-12-14 15:30:03 +01:00
parent 15c8838fea
commit c8a07cee36
10 changed files with 443 additions and 110 deletions

38
models/durations.go Normal file
View File

@ -0,0 +1,38 @@
package models
import "sort"
type Durations []*Duration
func (d Durations) Len() int {
return len(d)
}
func (d Durations) Less(i, j int) bool {
return d[i].Time.T().Before(d[j].Time.T())
}
func (d Durations) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
func (d *Durations) Sorted() *Durations {
sort.Sort(d)
return d
}
func (d *Durations) First() *Duration {
// assumes slice to be sorted
if d.Len() == 0 {
return nil
}
return (*d)[0]
}
func (d *Durations) Last() *Duration {
// assumes slice to be sorted
if d.Len() == 0 {
return nil
}
return (*d)[d.Len()-1]
}