forked from nleeper/goment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompare.go
141 lines (113 loc) · 2.75 KB
/
compare.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
package goment
import (
"regexp"
)
var inclusivityRegex = regexp.MustCompile("^[\\[\\(]{1}[\\]\\)]{1}$")
// IsBefore will check if a Goment is before another Goment.
func (g *Goment) IsBefore(args ...interface{}) bool {
var err error
var input *Goment
numArgs := len(args)
if numArgs == 0 {
input, err = New()
} else {
input, err = New(args[0])
}
if err != nil {
return false
}
if numArgs <= 1 {
return g.ToTime().Before(input.ToTime())
}
if units, ok := args[1].(string); ok {
return g.ToTime().Before(input.StartOf(units).ToTime())
}
return false
}
// IsAfter will check if a Goment is after another Goment.
func (g *Goment) IsAfter(args ...interface{}) bool {
var err error
var input *Goment
numArgs := len(args)
if numArgs == 0 {
input, err = New()
} else {
input, err = New(args[0])
}
if err != nil {
return false
}
if numArgs <= 1 {
return g.ToTime().After(input.ToTime())
}
if units, ok := args[1].(string); ok {
return g.ToTime().After(input.EndOf(units).ToTime())
}
return false
}
// IsSame will check if a Goment is the same as another Goment.
func (g *Goment) IsSame(args ...interface{}) bool {
numArgs := len(args)
if numArgs > 0 {
input, err := New(args[0])
if err != nil {
return false
}
if numArgs == 1 {
return g.ToTime().Equal(input.ToTime())
}
if units, ok := args[1].(string); ok {
return g.StartOf(units).ToTime().Equal(input.StartOf(units).ToTime())
}
}
return false
}
// IsSameOrBefore will check if a Goment is before or the same as another Goment.
func (g *Goment) IsSameOrBefore(args ...interface{}) bool {
return g.IsSame(args...) || g.IsBefore(args...)
}
// IsSameOrAfter will check if a Goment is after or the same as another Goment.
func (g *Goment) IsSameOrAfter(args ...interface{}) bool {
return g.IsSame(args...) || g.IsAfter(args...)
}
// IsBetween will check if a Goment is between two other Goments.
func (g *Goment) IsBetween(args ...interface{}) bool {
numArgs := len(args)
if numArgs >= 2 {
units := ""
inclusivity := "()"
fromResult, toResult := false, false
from, err := New(args[0])
if err != nil {
return false
}
to, err := New(args[1])
if err != nil {
return false
}
if numArgs >= 3 {
if parsedUnits, ok := args[2].(string); ok {
units = parsedUnits
}
}
if numArgs == 4 {
if parsedInclusivity, ok := args[3].(string); ok {
if inclusivityRegex.MatchString(parsedInclusivity) {
inclusivity = parsedInclusivity
}
}
}
if inclusivity[0] == '(' {
fromResult = g.IsAfter(from, units)
} else {
fromResult = !g.IsBefore(from, units)
}
if inclusivity[1] == ')' {
toResult = g.IsBefore(to, units)
} else {
toResult = !g.IsAfter(to, units)
}
return fromResult && toResult
}
return false
}