-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.go
150 lines (131 loc) · 3.24 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
package timeseq
import "time"
// Interval indicates a continuous time range
type Interval struct {
NotBefore *time.Time
NotAfter *time.Time
}
// Contain returns if time t is in the interval
func (i Interval) Contain(t time.Time) bool {
if i.NotAfter != nil && t.After(*i.NotAfter) {
return false
}
if i.NotBefore != nil && t.Before(*i.NotBefore) {
return false
}
return true
}
// String returns the interval formatted using the RFC3339 format string
func (i *Interval) String() string {
return i.Format(time.RFC3339)
}
// Format returns a textual representation of the time value formatted according to layout
func (i *Interval) Format(layout string) string {
notBefore, notAfter := "nil", "nil"
if i.NotBefore != nil {
notBefore = i.NotBefore.Format(layout)
}
if i.NotAfter != nil {
notAfter = i.NotAfter.Format(layout)
}
return notBefore + "~" + notAfter
}
// BeginAt is alias of AfterOrEqual
func (i Interval) BeginAt(t time.Time) Interval {
return i.AfterOrEqual(t)
}
// EndAt is alias of BeforeOrEqual
func (i Interval) EndAt(t time.Time) Interval {
return i.BeforeOrEqual(t)
}
// BeforeOrEqual returns a new Interval which not before t
func (i Interval) BeforeOrEqual(t time.Time) Interval {
return Interval{
NotBefore: i.NotBefore,
NotAfter: &t,
}
}
// AfterOrEqual returns a new Interval which not after t
func (i Interval) AfterOrEqual(t time.Time) Interval {
return Interval{
NotBefore: &t,
NotAfter: i.NotAfter,
}
}
// Before returns a new Interval which before t
func (i Interval) Before(t time.Time) Interval {
t = t.Add(-1)
return Interval{
NotBefore: i.NotBefore,
NotAfter: &t,
}
}
// After returns a new Interval which after t
func (i Interval) After(t time.Time) Interval {
t = t.Add(1)
return Interval{
NotBefore: &t,
NotAfter: i.NotAfter,
}
}
// Truncate returns the result of rounding interval down to a multiple of d (since the zero time).
func (i Interval) Truncate(d time.Duration) Interval {
if i.NotBefore != nil {
t := (*i.NotBefore).Truncate(d)
if t.Before(*i.NotBefore) {
t = t.Add(d)
}
i.NotBefore = &t
}
if i.NotAfter != nil {
t := (*i.NotAfter).Truncate(d)
i.NotAfter = &t
}
return i
}
// Duration returns the duration NotAfter - NotBefore,
// returns 0 if NotAfter is before or equal NotBefore,
// returns -1 if NotAfter or NotBefore if nil.
func (i Interval) Duration() time.Duration {
if i.NotBefore == nil || i.NotAfter == nil {
return -1
}
if !(*i.NotAfter).After(*i.NotBefore) {
return 0
}
return (*i.NotAfter).Sub(*i.NotBefore)
}
// BeginAt is alias of AfterOrEqual
func BeginAt(t time.Time) Interval {
return AfterOrEqual(t)
}
// EndAt is alias of BeforeOrEqual
func EndAt(t time.Time) Interval {
return BeforeOrEqual(t)
}
// BeforeOrEqual returns a new Interval which not before t
func BeforeOrEqual(t time.Time) Interval {
return Interval{
NotAfter: &t,
}
}
// AfterOrEqual returns a new Interval which not after t
func AfterOrEqual(t time.Time) Interval {
return Interval{
NotBefore: &t,
}
}
// Before returns a new Interval which before t
func Before(t time.Time) Interval {
t = t.Add(-1)
return Interval{
NotAfter: &t,
}
}
// After returns a new Interval which after t
func After(t time.Time) Interval {
t = t.Add(1)
return Interval{
NotBefore: &t,
}
}