From 0f88864200a132b64c5ececff848f0483f3c972c Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 3 Apr 2022 19:10:55 -0700 Subject: [PATCH] add changes to changeover code based on unit tests --- go.mod | 2 +- msp/changeover.go | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index d24e67c..d0ed8dc 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/taigrr/most-specific-period -go 1.17 +go 1.18 diff --git a/msp/changeover.go b/msp/changeover.go index ff08448..268e3dc 100644 --- a/msp/changeover.go +++ b/msp/changeover.go @@ -11,11 +11,20 @@ func GetChangeOvers(periods ...Period) (changeovers []time.Time) { timeStamps = append(timeStamps, x.GetEndTime()) timeStamps = append(timeStamps, x.GetStartTime()) } + if len(timeStamps) == 0 { + return + } sort.Slice(timeStamps, func(i, j int) bool { return timeStamps[i].Before(timeStamps[j]) }) - + // timeStamps is sorted, so this will always result in an unused time + // struct, as it's before the first + previousTs := timeStamps[0].Add(-10 * time.Nanosecond) for _, ts := range timeStamps { + if ts.Equal(previousTs) { + continue + } + previousTs = ts before := ts.Add(-1 * time.Nanosecond) after := ts.Add(1 * time.Nanosecond) from, _ := MostSpecificPeriod(before, periods...)