-
Notifications
You must be signed in to change notification settings - Fork 7
/
timespan.go
123 lines (101 loc) · 3 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
package spaniel
import (
"encoding/json"
"time"
)
// TimeSpan represents a simple span of time, with no additional properties. It should be constructed with NewEmpty.
type TimeSpan struct {
start time.Time
end time.Time
startType EndPointType
endType EndPointType
}
// Start returns the start time of a span
func (ts TimeSpan) Start() time.Time { return ts.start }
// End returns the end time of a span
func (ts TimeSpan) End() time.Time { return ts.end }
// StartType returns the type of the start of the interval (Open in this case)
func (ts TimeSpan) StartType() EndPointType { return ts.startType }
// EndType returns the type of the end of the interval (Closed in this case)
func (ts TimeSpan) EndType() EndPointType { return ts.endType }
// String returns a string representation of a timespan
func (ts TimeSpan) String() string {
s := ""
if ts.StartType() == Closed {
s += "["
} else {
s += "("
}
s += ts.Start().String()
if ts.Start() != ts.End() {
s += ","
s += ts.End().String()
}
if ts.EndType() == Closed {
s += "]"
} else {
s += ")"
}
return s
}
// MarshalJSON implements json.Marshal
func (ts TimeSpan) MarshalJSON() ([]byte, error) {
o := struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
StartIncluded bool `json:"start_included"`
EndIncluded bool `json:"end_included"`
}{
Start: ts.start,
End: ts.end,
}
o.StartIncluded = endPointInclusionMarshal(ts.startType)
o.EndIncluded = endPointInclusionMarshal(ts.endType)
return json.Marshal(o)
}
// UnmarshalJSON implements json.Unmarshal
func (ts *TimeSpan) UnmarshalJSON(b []byte) (err error) {
var i struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
StartIncluded bool `json:"start_included"`
EndIncluded bool `json:"end_included"`
}
err = json.Unmarshal(b, &i)
if err != nil {
return err
}
ts.start = i.Start
ts.end = i.End
ts.startType = endPointInclusionUnmarhsal(i.StartIncluded)
ts.endType = endPointInclusionUnmarhsal(i.EndIncluded)
return
}
func endPointInclusionMarshal(e EndPointType) bool {
if e == Open {
return false
}
return true
}
func endPointInclusionUnmarhsal(b bool) EndPointType {
if b == true {
return Closed
}
return Open
}
// NewWithTypes creates a span with just a start and end time, and associated types, and is used when no handlers are provided to Union or Intersection.
func NewWithTypes(start, end time.Time, startType, endType EndPointType) *TimeSpan {
return &TimeSpan{start, end, startType, endType}
}
// NewInstant creates a span with just a single time.
func NewInstant(time time.Time) *TimeSpan {
return New(time, time)
}
// New creates a span with a start and end time, with the types set to [] for instants and [) for spans.
func New(start time.Time, end time.Time) *TimeSpan {
if start.Equal(end) {
// An instantaneous event has to be Closed (i.e. inclusive)
return NewWithTypes(start, end, Closed, Closed)
}
return NewWithTypes(start, end, Closed, Open)
}