-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtimespan.go
166 lines (142 loc) · 4.09 KB
/
timespan.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
//Package timespan provides functionality for handling intervals of time.
package timespan
import (
"time"
)
//Span represents an inclusive range between two time instants.
//
//The zero value of type span has both start and end times set to the zero value
//of type Time. The zero value is returned by the Intersection and Gap methods
//when there is no span fitting their purposes.
type Span struct {
start, end time.Time
}
//New creates a new span with the given start instant and duration.
func New(t time.Time, d time.Duration) Span {
start := t
end := t.Add(d)
if end.Before(t) {
start, end = end, start
}
return Span{
start: start,
end: end,
}
}
//Start returns the time instant at the start of s.
func (s Span) Start() time.Time {
return s.start
}
//End returns the time instant at the end of s.
func (s Span) End() time.Time {
return s.end
}
//Duration returns the length of time represented by s.
func (s Span) Duration() time.Duration {
return s.end.Sub(s.start)
}
//After reports whether s begins after t.
func (s Span) After(t time.Time) bool {
return s.start.After(t)
}
//Before reports whether s ends before t.
func (s Span) Before(t time.Time) bool {
return s.end.Before(t)
}
//Borders reports whether s and r are contiguous time intervals.
func (s Span) Borders(r Span) bool {
return s.start.Equal(r.end) || s.end.Equal(r.start)
}
//ContainsTime reports whether t is within s.
func (s Span) ContainsTime(t time.Time) bool {
return !(t.Before(s.start) || t.After(s.end))
}
//Contains reports whether r is entirely within s.
func (s Span) Contains(r Span) bool {
return s.ContainsTime(r.start) && s.ContainsTime(r.end)
}
//Encompass returns the minimum span that fully contains both r and s.
func (s Span) Encompass(r Span) Span {
return Span{
start: tmin(s.start, r.start),
end: tmax(s.end, r.end),
}
}
//Equal reports whether s and r represent the same time intervals, ignoring
//the locations of the times.
func (s Span) Equal(r Span) bool {
return s.start.Equal(r.start) && s.end.Equal(r.end)
}
//Follows reports whether s begins after or at the end of r.
func (s Span) Follows(r Span) bool {
return !s.start.Before(r.end)
}
//Gap returns a span corresponding to the period between s and r.
//If s and r have a non-zero overlap, a zero span is returned.
func (s Span) Gap(r Span) Span {
if s.Overlaps(r) {
return Span{}
}
return Span{
start: tmin(s.end, r.end),
end: tmax(s.start, r.start),
}
}
//Intersection returns both a span corresponding to the non-zero overlap of
//s and r and a bool indicating whether such an overlap existed.
//If s and r do not overlap, a zero span is returned with false.
func (s Span) Intersection(r Span) (Span, bool) {
if !s.Overlaps(r) {
return Span{}, false
}
return Span{
start: tmax(s.start, r.start),
end: tmin(s.end, r.end),
}, true
}
//IsZero reports whether s represents the zero-length span starting and ending
//on January 1, year 1, 00:00:00 UTC.
func (s Span) IsZero() bool {
return s.start.IsZero() && s.end.IsZero()
}
//Offset returns s with its start time offset by d. It is equivalent to
//Newspan(s.Start().Add(d), s.Duration()).
func (s Span) Offset(d time.Duration) Span {
return Span{
start: s.start.Add(d),
end: s.end.Add(d),
}
}
//OffsetDate returns s with its start time offset by the given years, months,
//and days. It is equivalent to
//Newspan(s.Start().AddDate(years, months, days), s.Duration()).
func (s Span) OffsetDate(years, months, days int) Span {
d := s.Duration()
t := s.start.AddDate(years, months, days)
return Span{
start: t,
end: t.Add(d),
}
}
//Overlaps reports whether s and r intersect for a non-zero duration.
func (s Span) Overlaps(r Span) bool {
return s.start.Before(r.end) && s.end.After(r.start)
}
//Precedes reports whether s ends before or at the start of r.
func (s Span) Precedes(r Span) bool {
return !s.end.After(r.start)
}
//tmax returns the later of two time instants.
func tmax(t, u time.Time) time.Time {
if t.After(u) {
return t
}
return u
}
//tmin returns the earlier of two time instants.
func tmin(t, u time.Time) time.Time {
if t.Before(u) {
return t
}
return u
}