feat: Completed functionality for generating a period timeline

This commit is contained in:
Ethan Holz
2022-10-14 14:17:50 -05:00
parent 9698d90308
commit 8612f90d46
4 changed files with 213 additions and 25 deletions

31
msp/timeline.go Normal file
View File

@@ -0,0 +1,31 @@
package msp
import "fmt"
// Outputs a formatted timeline of periods
func GenerateTimeline(periods ...Period) (out []string) {
if len(periods) == 0 {
out = []string{}
return out
}
periodsByID := make(map[string]Period)
ids := FlattenPeriods(periods...)
for _, val := range periods {
id := val.GetIdentifier()
periodsByID[id] = val
}
start := periodsByID[ids[0]].GetStartTime()
for _, val := range ids {
next, err := GetNextChangeOver(start, periods...)
if err == nil {
if next.Equal(periodsByID[val].GetStartTime()) {
start = periodsByID[val].GetStartTime()
next = periodsByID[val].GetEndTime()
}
frame := fmt.Sprintf("%s\t%s\t%s\n", val, start, next)
out = append(out, frame)
start = next
}
}
return out
}