-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval.go
More file actions
73 lines (63 loc) · 1.92 KB
/
Copy pathinterval.go
File metadata and controls
73 lines (63 loc) · 1.92 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
package timex
import (
"strings"
"go.gh.ink/toolbox/expr"
)
func NewInterval(start, end Time, startIncluded, endIncluded bool) Interval {
return Interval{
start: start,
startIncluded: startIncluded,
end: end,
endIncluded: endIncluded,
}
}
func (i Interval) Before(t Time) bool {
if i.end.Before(t) {
return true
}
if i.end.Equal(t) {
return !i.endIncluded
}
return false
}
func (i Interval) After(t Time) bool {
if i.start.After(t) {
return true
}
if i.start.Equal(t) {
return !i.startIncluded
}
return false
}
func (i Interval) Contain(t Time) bool {
leftOk := i.start.Before(t) || (i.startIncluded && i.start.Equal(t))
rightOk := i.end.After(t) || (i.endIncluded && i.end.Equal(t))
return leftOk && rightOk
}
func (i Interval) Start() (start Time, included bool) {
return i.start, i.startIncluded
}
func (i Interval) End() (end Time, included bool) {
return i.end, i.endIncluded
}
// IsZero reports whether the interval is the zero value: both bounds are the
// zero (finite) time and both bounds excluded. It lets reflection-based
// zero-checkers (e.g. ORM helpers) treat Interval as an opaque value instead of
// recursing into its unexported fields.
func (i Interval) IsZero() bool {
return i.start.IsZero() && i.end.IsZero() && !i.startIncluded && !i.endIncluded
}
// String renders the interval in mathematical notation using each bound's
// human-readable String form (Go's default time layout, or the infinity
// sentinels) — e.g. "[2024-01-01 00:00:00 +0000 UTC,positive infinite)".
// Brackets encode inclusivity: "[" / "]" included, "(" / ")" excluded. For the
// machine-parseable RFC 3339 form, use MarshalText.
func (i Interval) String() string {
var b strings.Builder
b.WriteString(expr.Ternary(i.startIncluded, "[", "("))
b.WriteString(i.start.String())
b.WriteByte(',')
b.WriteString(i.end.String())
b.WriteString(expr.Ternary(i.endIncluded, "]", ")"))
return b.String()
}