-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime_range.go
More file actions
151 lines (126 loc) · 3.17 KB
/
time_range.go
File metadata and controls
151 lines (126 loc) · 3.17 KB
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
package accounting
import (
"bytes"
"fmt"
"sort"
"time"
)
var (
past = time.Date(0, 1, 1, 0, 0, 0, 0, time.UTC)
future = time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC)
EmptyTimeRange = &TimeRange{
start: time.Date(2000, 4, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
}
)
type TimeRange struct {
start time.Time
end time.Time
}
func NewTimeRange(start, end time.Time) *TimeRange {
return &TimeRange{
start: start,
end: end,
}
}
func UpTo(end time.Time) *TimeRange {
return &TimeRange{start: past, end: end}
}
func StartingOn(start time.Time) *TimeRange {
return &TimeRange{start: start, end: future}
}
func (tr *TimeRange) Start() time.Time {
return tr.start
}
func (tr *TimeRange) End() time.Time {
return tr.end
}
func (tr *TimeRange) Includes(timepoint time.Time) bool {
return !timepoint.Before(tr.start) && !timepoint.After(tr.end)
}
func (tr *TimeRange) IncludesRange(arg *TimeRange) bool {
return tr.Includes(arg.start) || tr.Includes(arg.end)
}
func (tr *TimeRange) Overlaps(arg *TimeRange) bool {
return arg.Includes(tr.start) || arg.Includes(tr.end) || tr.IncludesRange(arg)
}
func (tr *TimeRange) Equals(arg interface{}) bool {
switch arg.(type) {
case TimeRange:
other := arg.(*TimeRange)
return tr.start.Equal(other.start) && tr.end.Equal(other.end)
default:
return false
}
}
func (tr *TimeRange) Gap(arg *TimeRange) *TimeRange {
if tr.Overlaps(arg) {
return EmptyTimeRange
}
var lower, higher *TimeRange
if tr.CompareTo(arg) < 0 {
lower = tr
higher = arg
} else {
lower = arg
higher = tr
}
return NewTimeRange(lower.end.AddDate(0, 0, 1), higher.start.AddDate(0, 0, -1))
}
func (tr *TimeRange) CompareTo(arg interface{}) int {
other := arg.(*TimeRange)
if !tr.start.Equal(other.start) {
start, _ := tr.start.MarshalBinary()
ostart, _ := other.start.MarshalBinary()
return bytes.Compare(start, ostart)
}
start, _ := tr.end.MarshalBinary()
ostart, _ := other.end.MarshalBinary()
return bytes.Compare(start, ostart)
}
func (tr *TimeRange) Abuts(arg *TimeRange) bool {
return !tr.Overlaps(arg) && tr.Gap(arg).isEmpty()
}
func (tr *TimeRange) PartitionedBy(args timeRangeSlice) bool {
sort.Sort(args)
if !tr.IsContigous(args) {
return false
}
combination, _ := tr.Combination(args)
return tr.Equals(combination)
}
func (tr TimeRange) Combination(args timeRangeSlice) (*TimeRange, error) {
sort.Sort(args)
if !tr.IsContigous(args) {
return nil, fmt.Errorf("unable to combine date reanges")
}
return NewTimeRange(args[0].start, args[len(args)-1].end), nil
}
func (tr *TimeRange) IsContigous(args timeRangeSlice) bool {
sort.Sort(args)
for i := 0; i < len(args)-1; i++ {
if !args[i].Abuts(args[i+1]) {
return false
}
}
return true
}
type timeRangeSlice []*TimeRange
func (p timeRangeSlice) Len() int {
return len(p)
}
func (p timeRangeSlice) Less(i, j int) bool {
return p[i].start.Before(p[j].start)
}
func (p timeRangeSlice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (tr *TimeRange) String() string {
if tr.isEmpty() {
return "empty date range"
}
return fmt.Sprintf("%s - %s", tr.start, tr.end)
}
func (tr *TimeRange) isEmpty() bool {
return tr.start.After(tr.end)
}