Go port of the Haskell Rampart library by Taylor Fausak.
This package provides types and functions for defining intervals and determining how they relate to each other. This can be useful to determine if an event happened during a certain time frame, or if two time frames overlap (and if so, how exactly they overlap).
go get github.com/francesconi/go-rampart
Any type that supports the operators <
<=
>=
>
.
a := rampart.NewInterval(2, 3)
b := rampart.NewInterval(3, 7)
rel := a.Relate(b)
// rel: RelationMeets
NewIntervalFunc
accepts any type as long as a comparison function is provided. The comparison function should return values as follows:
cmp(t1, t2) < 0 if t1 < t2
cmp(t1, t2) > 0 if t1 > t2
cmp(t1, t2) == 0 if t1 == t2
func compareTimes(t1, t2 time.Time) int {
return int(t1.Sub(t2))
}
a := rampart.NewIntervalFunc(
time.Date(2022, time.April, 1, 0, 0, 0, 0, time.UTC),
time.Date(2022, time.April, 8, 0, 0, 0, 0, time.UTC),
compareTimes,
)
b := rampart.NewIntervalFunc(
time.Date(2022, time.April, 6, 0, 0, 0, 0, time.UTC),
time.Date(2022, time.April, 15, 0, 0, 0, 0, time.UTC),
compareTimes,
)
rel := a.Relate(b)
// rel: RelationOverlaps