File size: 3,652 Bytes
6380833 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | package timeutil
import (
"reflect"
"slices"
"time"
)
type SimpleTimeline = Timeline[time.Time]
func NewSimpleTimeline(times []time.Time) SimpleTimeline {
wrapped := make([]Timed[time.Time], len(times))
for i, t := range times {
wrapped[i] = AsTimed(func(t time.Time) time.Time { return t })(t)
}
return NewTimeline(wrapped)
}
// AsTimed returns a function that converts a value of type T to a Timed value.
func AsTimed[T any](fn func(T) time.Time) func(T) Timed[T] {
return func(t T) Timed[T] {
return Timed[T]{
val: t,
fn: fn,
}
}
}
type Timed[T any] struct {
val T
fn func(T) time.Time
}
func (t Timed[T]) GetTime() time.Time {
return t.fn(t.val)
}
func (t Timed[T]) GetValue() T {
return t.val
}
func (t Timed[T]) Equal(other Timed[T]) bool {
if !t.GetTime().Equal(other.GetTime()) {
return false
}
return reflect.DeepEqual(t.GetValue(), other.GetValue())
}
type Timeline[T any] struct {
times []Timed[T]
}
func NewTimeline[T any](times []Timed[T]) Timeline[T] {
// sort copy of times ASC
times = slices.Clone(times)
slices.SortStableFunc(times, func(a, b Timed[T]) int {
return a.GetTime().Compare(b.GetTime())
})
return Timeline[T]{times: times}
}
func (t Timeline[T]) After(at time.Time) Timeline[T] {
times := make([]Timed[T], 0, len(t.times))
for _, t := range t.times {
if t.GetTime().After(at) {
times = append(times, t)
}
}
return NewTimeline(times)
}
func (t Timeline[T]) Before(at time.Time) Timeline[T] {
times := make([]Timed[T], 0, len(t.times))
for _, t := range t.times {
if t.GetTime().Before(at) {
times = append(times, t)
}
}
return NewTimeline(times)
}
func (t Timeline[T]) GetTimes() []time.Time {
times := make([]time.Time, len(t.times))
for i, t := range t.times {
times[i] = t.GetTime()
}
return times
}
func (t Timeline[T]) GetAt(idx int) Timed[T] {
return t.times[idx]
}
func (t Timeline[T]) GetBoundingPeriod() ClosedPeriod {
if len(t.times) == 0 {
return ClosedPeriod{
From: time.Time{},
To: time.Time{},
}
}
return ClosedPeriod{
From: t.times[0].GetTime(),
To: t.times[len(t.times)-1].GetTime(),
}
}
// GetClosedPeriods returns the closed periods between the times in the timeline.
func (t Timeline[T]) GetClosedPeriods() []ClosedPeriod {
if len(t.times) == 0 {
return []ClosedPeriod{}
}
if len(t.times) == 1 {
return []ClosedPeriod{
{
From: t.times[0].GetTime(),
To: t.times[0].GetTime(),
},
}
}
periods := make([]ClosedPeriod, 0, len(t.times)-1)
for i := 0; i < len(t.times)-1; i++ {
periods = append(periods, ClosedPeriod{From: t.times[i].GetTime(), To: t.times[i+1].GetTime()})
}
return periods
}
// GetOpenPeriods returns the open periods between the times in the timeline.
// For non-empty timelines, the first and last periods will be open at the start and end of the timeline respectively while the rest will effectively be closed.
func (t Timeline[T]) GetOpenPeriods() []OpenPeriod {
if len(t.times) == 0 {
return []OpenPeriod{}
}
periods := make([]OpenPeriod, 0, len(t.times)+1)
// First period: open at start, closed at first time
firstTime := t.times[0].GetTime()
periods = append(periods, OpenPeriod{
From: nil,
To: &firstTime,
})
// Middle periods: closed at both ends
for i := 0; i < len(t.times)-1; i++ {
from := t.times[i].GetTime()
to := t.times[i+1].GetTime()
periods = append(periods, OpenPeriod{
From: &from,
To: &to,
})
}
// Last period: closed at last time, open at end
lastTime := t.times[len(t.times)-1].GetTime()
periods = append(periods, OpenPeriod{
From: &lastTime,
To: nil,
})
return periods
}
|