mirror of
https://github.com/taigrr/most-specific-period.git
synced 2026-04-02 03:38:41 -07:00
changeover features in progress
This commit is contained in:
51
msp/changeover.go
Normal file
51
msp/changeover.go
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
package msp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetChangeOvers(periods ...Period) (changeovers []time.Time) {
|
||||||
|
timeStamps := []time.Time{}
|
||||||
|
for _, x := range periods {
|
||||||
|
timeStamps = append(timeStamps, x.GetEndTime())
|
||||||
|
timeStamps = append(timeStamps, x.GetStartTime())
|
||||||
|
}
|
||||||
|
sort.Slice(timeStamps, func(i, j int) bool {
|
||||||
|
return timeStamps[i].Before(timeStamps[j])
|
||||||
|
})
|
||||||
|
|
||||||
|
for _, ts := range timeStamps {
|
||||||
|
before := ts.Add(-1 * time.Nanosecond)
|
||||||
|
after := ts.Add(1 * time.Nanosecond)
|
||||||
|
from, _ := MostSpecificPeriod(before, periods...)
|
||||||
|
to, _ := MostSpecificPeriod(after, periods...)
|
||||||
|
if from == to {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
changeovers = append(changeovers, ts)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetNextChangeOver(t time.Time, periods ...Period) (ts time.Time, err error) {
|
||||||
|
changeOvers := GetChangeOvers(periods...)
|
||||||
|
for _, ts := range changeOvers {
|
||||||
|
if ts.After(t) {
|
||||||
|
return ts, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return time.Unix(0, 0), ErrNoNextChangeover
|
||||||
|
}
|
||||||
|
|
||||||
|
func FlattenPeriods(periods ...Period) (ids []string) {
|
||||||
|
changeovers := GetChangeOvers(periods...)
|
||||||
|
for _, c := range changeovers {
|
||||||
|
id, err := MostSpecificPeriod(c, periods...)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -9,4 +9,6 @@ var (
|
|||||||
ErrEndAfterStart = errors.New("error: start time is after end time")
|
ErrEndAfterStart = errors.New("error: start time is after end time")
|
||||||
// ErrNoValidPeriods occurs when an empty set of periods is passed or when ll periods are invalid
|
// ErrNoValidPeriods occurs when an empty set of periods is passed or when ll periods are invalid
|
||||||
ErrNoValidPeriods = errors.New("error: no valid periods available")
|
ErrNoValidPeriods = errors.New("error: no valid periods available")
|
||||||
|
// ErrNoNextChangeover occurs when GetNextChangeover is called but there are no changeovers after t
|
||||||
|
ErrNoNextChangeover = errors.New("error: no valid periods available")
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user