snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
build / build (push) Failing after 3s
Some checks failed
build / build (push) Failing after 3s
This commit is contained in:
41
time/range.go
Normal file
41
time/range.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package time
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/jmattheis/go-timemath"
|
||||
"github.com/traggo/server/model"
|
||||
)
|
||||
|
||||
func ranges(from time.Time, to time.Time, interval model.Interval) []StaticRange {
|
||||
switch interval {
|
||||
case model.IntervalSingle:
|
||||
return []StaticRange{{From: from, To: to}}
|
||||
case model.IntervalHourly:
|
||||
return rangeForUnit(from, to, timemath.Hour)
|
||||
case model.IntervalDaily:
|
||||
return rangeForUnit(from, to, timemath.Day)
|
||||
case model.IntervalWeekly:
|
||||
return rangeForUnit(from, to, timemath.Week)
|
||||
case model.IntervalMonthly:
|
||||
return rangeForUnit(from, to, timemath.Month)
|
||||
case model.IntervalYearly:
|
||||
return rangeForUnit(from, to, timemath.Year)
|
||||
default:
|
||||
panic("unknown interval type")
|
||||
}
|
||||
}
|
||||
|
||||
func rangeForUnit(from time.Time, to time.Time, u timemath.Unit) []StaticRange {
|
||||
var result []StaticRange
|
||||
newFrom := from
|
||||
for newFrom.Before(to) {
|
||||
newTo := timemath.Second.Subtract(u.Add(newFrom, 1), 1)
|
||||
if newTo.After(to) {
|
||||
newTo = to
|
||||
}
|
||||
result = append(result, StaticRange{From: newFrom, To: newTo})
|
||||
newFrom = timemath.Second.Add(newTo, 1)
|
||||
}
|
||||
return result
|
||||
}
|
||||
Reference in New Issue
Block a user