-
Notifications
You must be signed in to change notification settings - Fork 10
/
time.go
215 lines (192 loc) · 4.23 KB
/
time.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package meta
import (
"database/sql/driver"
"reflect"
"regexp"
"strconv"
"time"
)
//
// Time
//
type Time struct {
Val time.Time
Nullity
Presence
Path string
}
type TimeOptions struct {
Required bool
DiscardBlank bool
Null bool
Format []string
}
func NewTime(t time.Time) Time {
return Time{t, Nullity{false}, Presence{true}, ""}
}
func (t *Time) ParseOptions(tag reflect.StructTag) interface{} {
opts := &TimeOptions{
Required: false,
DiscardBlank: true,
Null: false,
Format: []string{time.RFC3339, "expression"},
}
if tag.Get("meta_required") == "true" {
opts.Required = true
}
if tag.Get("meta_null") == "true" {
opts.Null = true
}
if tag.Get("meta_discard_blank") == "false" {
opts.DiscardBlank = false
}
if tag.Get("meta_format") != "" {
opts.Format = []string{tag.Get("meta_format")}
}
return opts
}
func (t *Time) JSONValue(path string, i interface{}, options interface{}) Errorable {
t.Path = path
if i == nil {
return t.FormValue("", options)
}
switch value := i.(type) {
case time.Time:
if value.IsZero() {
opts := options.(*TimeOptions)
if opts.Null {
t.Present = true
t.Null = true
return nil
}
if opts.Required {
return ErrBlank
}
if !opts.DiscardBlank {
t.Present = true
return ErrBlank
}
return nil
}
t.Present = true
t.Val = value
return nil
case string:
return t.FormValue(value, options)
}
return ErrTime
}
type expressionParser struct {
*regexp.Regexp
Parse func([]string) (time.Time, bool)
}
var timeExpressionParsers = []expressionParser{
{
Regexp: regexp.MustCompile(`^(\d+)_(year|month|week|day|hour|minute|second|nanosecond)s?_(ago|from_now)$`),
Parse: func(matches []string) (time.Time, bool) {
delta, err := strconv.Atoi(matches[1])
if err != nil {
return time.Time{}, false
}
if matches[3] == "ago" {
delta = -delta
}
switch matches[2] {
case "year":
return time.Now().AddDate(delta, 0, 0), true
case "month":
return time.Now().AddDate(0, delta, 0), true
case "week":
return time.Now().AddDate(0, 0, delta*7), true
case "day":
return time.Now().AddDate(0, 0, delta), true
case "hour":
return time.Now().Add(time.Duration(delta) * time.Hour), true
case "minute":
return time.Now().Add(time.Duration(delta) * time.Minute), true
case "second":
return time.Now().Add(time.Duration(delta) * time.Second), true
case "nanosecond":
return time.Now().Add(time.Duration(delta) * time.Nanosecond), true
}
return time.Time{}, false
},
},
{
Regexp: regexp.MustCompile(`^now$`),
Parse: func(matches []string) (time.Time, bool) {
return time.Now(), true
},
},
{
Regexp: regexp.MustCompile(`^today$`),
Parse: func(matches []string) (time.Time, bool) {
return time.Now().Truncate(24 * time.Hour), true
},
},
{
Regexp: regexp.MustCompile(`^yesterday$`),
Parse: func(matches []string) (time.Time, bool) {
return time.Now().Truncate(24*time.Hour).AddDate(0, 0, -1), true
},
},
{
Regexp: regexp.MustCompile(`^tomorrow$`),
Parse: func(matches []string) (time.Time, bool) {
return time.Now().Truncate(24*time.Hour).AddDate(0, 0, 1), true
},
},
}
func (t *Time) FormValue(value string, options interface{}) Errorable {
opts := options.(*TimeOptions)
if value == "" {
if opts.Null {
t.Present = true
t.Null = true
return nil
}
if opts.Required {
return ErrBlank
}
if !opts.DiscardBlank {
t.Present = true
return ErrBlank
}
return nil
}
for _, format := range opts.Format {
switch format {
case "expression":
for _, parser := range timeExpressionParsers {
submatches := parser.Regexp.FindStringSubmatch(value)
if len(submatches) == 0 {
continue
}
if v, ok := parser.Parse(submatches); ok {
t.Val = v
t.Present = true
return nil
}
}
default:
if v, err := time.Parse(format, value); err == nil {
t.Val = v
t.Present = true
return nil
}
}
}
return ErrTime
}
func (t Time) Value() (driver.Value, error) {
if t.Present && !t.Null {
return t.Val, nil
}
return nil, nil
}
func (t Time) MarshalJSON() ([]byte, error) {
if t.Present && !t.Null {
return MetaJson.Marshal(t.Val)
}
return nullString, nil
}