mirror of
https://github.com/taigrr/most-specific-period.git
synced 2026-04-02 03:38:41 -07:00
32 lines
755 B
Go
32 lines
755 B
Go
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
|
|
}
|