forked from retailify/go-interval
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.go
389 lines (316 loc) · 10.2 KB
/
interval.go
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
Copyright © 2018 Thomas Meitz <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Package interval contains methods to analyze time intervals and relations between two intervals for go.
//
// The package is inspired by Allen's interval algebra
// and implements the described 13 basic relations between time intervals.
//
// See also
//
// http://www.ics.uci.edu/~alspaugh/cls/shr/allen.html
package interval
import (
"errors"
"fmt"
"time"
)
// TimeInterval is a struct that contains the start and end time for a interval
type TimeInterval struct {
startTime *time.Time
endTime *time.Time
}
// State type represents the relation type of two TimeInterval's
type State int
const (
//Unknown relation
Unknown State = 0
//Precedes relation
Precedes State = 1
// Meets relation
Meets State = 2
// Overlaps relation
Overlaps State = 3
// FinishedBy relation
FinishedBy State = 4
// Contains relation
Contains State = 5
// Starts relation
Starts State = 6
// Equals relation
Equals State = 7
// StartedBy relation
StartedBy State = 8
// During relation
During State = 9
// Finishes relation
Finishes State = 10
// OverlappedBy relation
OverlappedBy State = 11
// MetBy relation
MetBy State = 12
// PrecededBy relation
PrecededBy State = 13
)
const (
// TimeIntervalError error message
TimeIntervalError = "interval must not nil or unknown error"
// TimeIntervalEmptyStartTimeError error message
TimeIntervalEmptyStartTimeError = "start time must not empty"
// TimeIntervalEmptyEndTimeError error message
TimeIntervalEmptyEndTimeError = "end time must not empty"
// TimeIntervalParseError error message
TimeIntervalParseError = "can't parse time string"
)
// MakeTimeInterval makes a new TimeInterval
func MakeTimeInterval(start, end *time.Time) (*TimeInterval, error) {
if start == nil {
return nil, errors.New(TimeIntervalEmptyStartTimeError)
}
if end == nil {
return nil, errors.New(TimeIntervalEmptyEndTimeError)
}
return &TimeInterval{startTime: start, endTime: end}, nil
}
// MakeTimeIntervalFromStrings makes a new TimeInterval from time strings formatted by format
func MakeTimeIntervalFromStrings(start, end, format string) (*TimeInterval, error) {
var startTime, endTime time.Time
var err error
if startTime, err = time.Parse(format, start); err != nil {
return nil, errors.New(TimeIntervalParseError)
}
if endTime, err = time.Parse(format, end); err != nil {
return nil, errors.New(TimeIntervalParseError)
}
return MakeTimeInterval(&startTime, &endTime)
}
// String returns the formatted interval dates
func (i *TimeInterval) String(format string) string {
return fmt.Sprintf("[%v, %v]", i.startTime.Format(format), i.endTime.Format(format))
}
// Start returns the start time of the interval
func (i *TimeInterval) Start() *time.Time {
return i.startTime
}
// End returns the end time of the interval
func (i *TimeInterval) End() *time.Time {
return i.endTime
}
func (i *TimeInterval) Add(d time.Duration) {
*i.startTime = i.startTime.Add(d)
*i.endTime = i.endTime.Add(d)
}
// Duration returns the time.Duration of the interval
func (i *TimeInterval) Duration() time.Duration {
return i.endTime.Sub(*i.startTime)
}
// Relation return the state of two intervals
func (i *TimeInterval) Relation(interval *TimeInterval, constraint time.Duration) (state State, err error) {
if interval == nil {
state, err = Unknown, errors.New(TimeIntervalError)
} else if i.Precedes(interval, constraint) {
state, err = Precedes, nil
} else if i.PrecededBy(interval, constraint) {
state, err = PrecededBy, nil
} else if i.Meets(interval, constraint) {
state, err = Meets, nil
} else if i.Overlaps(interval) {
state, err = Overlaps, nil
} else if i.MetBy(interval, constraint) {
state, err = MetBy, nil
} else if i.FinishedBy(interval) {
state, err = FinishedBy, nil
} else if i.Contains(interval) {
state, err = Contains, nil
} else if i.Starts(interval) {
state, err = Starts, nil
} else if i.Equals(interval) {
state, err = Equals, nil
} else if i.StartedBy(interval) {
state, err = StartedBy, nil
} else if i.During(interval) {
state, err = During, nil
} else if i.Finishes(interval) {
state, err = Finishes, nil
} else if i.OverlappedBy(interval) {
state, err = OverlappedBy, nil
}
return
}
// Equals checks two time intervals are equal or not
//
// converse relation of Equals is Equals
func (i *TimeInterval) Equals(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.Equal(*interval.startTime) && i.endTime.Equal(*interval.endTime) {
return true
}
return false
}
// Meets returns true if interval A meets B
//
// converse relation of MetBy
func (i *TimeInterval) Meets(interval *TimeInterval, constraint time.Duration) bool {
if interval == nil {
return false
}
// result := i.endTime.Add(constraint) // .Equal(*interval.startTime)
// fmt.Println(i.String(time.RFC3339), interval.String(time.RFC3339), result)
if i.endTime.Before(*interval.startTime) &&
i.endTime.Add(constraint).Equal(*interval.startTime) {
return true
}
return false
}
// MetBy returns true if interval A is met by B
//
// converse relation of Met
func (i *TimeInterval) MetBy(interval *TimeInterval, constraint time.Duration) bool {
if interval == nil {
return false
}
// result := interval.endTime.Add(constraint)
// fmt.Println(i.String(time.RFC3339), interval.String(time.RFC3339), result)
if i.startTime.After(*interval.endTime) &&
interval.endTime.Add(constraint).Equal(*i.startTime) {
return true
}
return false
}
// Precedes returns true if interval A precedes B by duration
//
// converse relation of PrecedesBy
func (i *TimeInterval) Precedes(interval *TimeInterval, constraint time.Duration) bool {
if interval == nil {
return false
}
if result := interval.startTime.Sub(*i.endTime); result <= constraint {
return false
}
return true
}
// Overlaps returns true if interval A overlaps B
//
// converse relation of OverlappedBy
func (i *TimeInterval) Overlaps(interval *TimeInterval) bool {
if interval == nil {
return false
}
if (i.startTime.Before(*interval.startTime) || i.startTime.Before(*interval.endTime)) && (i.endTime.After(*interval.startTime)) {
return true
}
return false
}
// FinishedBy returns true if interval A is finished by B
//
// converse relation of OverlappedBy
func (i *TimeInterval) FinishedBy(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.Before(*interval.startTime) && i.endTime.Equal(*interval.endTime) {
return true
}
return false
}
// Contains returns true if interval A contains B
//
// converse relation to During
func (i *TimeInterval) Contains(interval *TimeInterval) bool {
if interval == nil {
return false
}
if (i.startTime.Before(*interval.startTime) || i.startTime.Equal(*interval.startTime)) && (i.endTime.After(*interval.endTime) || i.endTime.Equal(*interval.endTime)) {
return true
}
return false
}
// Starts returns true if interval A's startTime is identical with interval B's startTime and interval A's duration
// is greater than interval B's duration
//
// converse relation of StartedBy
func (i *TimeInterval) Starts(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.Equal(*interval.startTime) && i.endTime.Before(*interval.endTime) {
return true
}
return false
}
// StartedBy returns true if interval A startTime is identical with interval B startTime and interval A's duration
// is shorter than interval B's duration
//
// converse relation of Starts
func (i *TimeInterval) StartedBy(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.Equal(*interval.startTime) && i.endTime.After(*interval.endTime) {
return true
}
return false
}
// During returns true if interval A is contained by interval B
//
// converse relation of Contains
func (i *TimeInterval) During(interval *TimeInterval) bool {
if interval == nil {
return false
}
if (i.startTime.After(*interval.startTime) || i.startTime.Equal(*interval.startTime)) && (i.endTime.Before(*interval.endTime) || i.endTime.Equal(*interval.endTime)) {
return true
}
return false
}
// Finishes returns true if interval A's endTime is identical with B's endTime and B's startTime is greater than
// A's startTime
//
// converse relation of FinishedBy
func (i *TimeInterval) Finishes(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.After(*interval.startTime) && i.endTime.Equal(*interval.endTime) {
return true
}
return false
}
// OverlappedBy returns true if interval A's startTime is contained in interval B and greater than B's startTime
// and A's endTime is greater than B's endTime
//
// converse relation to Overlaps
func (i *TimeInterval) OverlappedBy(interval *TimeInterval) bool {
if interval == nil {
return false
}
if i.startTime.After(*interval.startTime) && i.endTime.After(*interval.endTime) {
return true
}
return false
}
// PrecededBy returns true if interval A is preceded by B. That's the case if interval B's endTime + constraint is
// greater than A's startTime
//
// converse relation of Precedes
func (i *TimeInterval) PrecededBy(interval *TimeInterval, constraint time.Duration) bool {
if interval == nil {
return false
}
if result := i.startTime.Sub(*interval.endTime); result <= constraint {
return false
}
return true
}