-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgt_null_date.go
321 lines (269 loc) · 8.19 KB
/
gt_null_date.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package gt
import (
"database/sql/driver"
"fmt"
"time"
)
/*
Shortcut for making a date from a time:
inst := time.Now()
date := gt.NullDateFrom(inst.Date())
Reversible:
date == gt.NullDateFrom(date.Date())
Note that `gt.NullDateFrom(0, 0, 0)` returns a zero value which is considered
empty/null, but NOT equivalent to `time.Time{}`. The equivalent of zero time is
`gt.NullDateFrom(1, 1, 1)`.
*/
func NullDateFrom(year int, month time.Month, day int) NullDate {
return NullDate{year, month, day}
}
// Shortcut for `gt.NullTimeNow().NullDate()`.
func NullDateNow() NullDate {
return NullTimeNow().NullDate()
}
/*
Shortcut: parses successfully or panics. Should be used only in root scope. When
error handling is relevant, use `.Parse`.
*/
func ParseNullDate(src string) (val NullDate) {
try(val.Parse(src))
return
}
/*
Civil date without time. Corresponds to SQL type `date` and HTML input with
`type="date"`. Zero value is considered empty in text, and null in JSON and
SQL. Features:
* Reversible encoding/decoding in text. Zero value is "".
* Reversible encoding/decoding in JSON. Zero value is `null`.
* Reversible encoding/decoding in SQL. Zero value is `null`.
* Text encoding uses the ISO 8601 extended calendar date format: "0001-02-03".
* Text decoding supports date-only strings and full RFC3339 timestamps.
* Convertible to and from `gt.NullTime`.
Caution: `gt.NullDate{}` or `gt.NullDate{0, 0, 0}` is considered empty/null, but
when converted to `time.Time` or `gt.NullTime`, it's NOT equivalent to the zero
time. The equivalent of zero time is `gt.NullDate{1, 1, 1}`.
*/
type NullDate struct {
Year int `json:"year" db:"year"`
Month time.Month `json:"month" db:"month"`
Day int `json:"day" db:"day"`
}
var (
_ = Encodable(NullDate{})
_ = Decodable((*NullDate)(nil))
)
// Implement `gt.Zeroable`. Equivalent to `reflect.ValueOf(self).IsZero()`.
func (self NullDate) IsZero() bool { return self == NullDate{} }
// Implement `gt.Nullable`. True if zero.
func (self NullDate) IsNull() bool { return self.IsZero() }
/*
Implement `gt.Getter`. If zero, returns `nil`, otherwise uses `.TimeUTC` to
return a timestamp suitable for SQL encoding.
*/
func (self NullDate) Get() any {
if self.IsNull() {
return nil
}
return self.TimeUTC()
}
// Implement `gt.Setter`, using `.Scan`. Panics on error.
func (self *NullDate) Set(src any) { try(self.Scan(src)) }
// Implement `gt.Zeroer`, zeroing the receiver.
func (self *NullDate) Zero() {
if self != nil {
*self = NullDate{}
}
}
/*
Implement `fmt.Stringer`. If zero, returns an empty string. Otherwise returns a
text representation in the standard machine-readable ISO 8601 format.
*/
func (self NullDate) String() string {
if self.IsNull() {
return ``
}
return bytesString(self.AppendTo(nil))
}
/*
Implement `gt.Parser`. If the input is empty, zeroes the receiver. Otherwise
requires an ISO 8601 date representation, one of:
* Extended calendar date: "2006-01-02"
* RFC3339 (default Go timestamp format): "2006-01-02T15:04:05Z07:00"
*/
func (self *NullDate) Parse(src string) error {
if len(src) == 0 {
self.Zero()
return nil
}
var val time.Time
var err error
// Too restrictive. TODO fuzzier detection.
if len(src) == len(dateFormat) {
val, err = time.Parse(dateFormat, src)
} else {
val, err = time.Parse(timeFormat, src)
}
if err != nil {
return err
}
self.SetTime(val)
return nil
}
// Implement `gt.AppenderTo`, using the same representation as `.String`.
func (self NullDate) AppendTo(buf []byte) []byte {
if self.IsNull() {
return buf
}
// `time.Time.AppendFormat` doesn't seem to do this.
buf = Raw(buf).Grow(dateStrLen)
return self.TimeUTC().AppendFormat(buf, dateFormat)
}
/*
Implement `encoding.TextMarhaler`. If zero, returns nil. Otherwise returns the
same representation as `.String`.
*/
func (self NullDate) MarshalText() ([]byte, error) {
if self.IsNull() {
return nil, nil
}
return self.AppendTo(nil), nil
}
// Implement `encoding.TextUnmarshaler`, using the same algorithm as `.Parse`.
func (self *NullDate) UnmarshalText(src []byte) error {
return self.Parse(bytesString(src))
}
/*
Implement `json.Marshaler`. If zero, returns bytes representing `null`.
Otherwise returns bytes representing a JSON string with the same text as in
`.String`.
*/
func (self NullDate) MarshalJSON() ([]byte, error) {
if self.IsNull() {
return bytesNull, nil
}
var arr [dateStrLen + 2]byte
buf := arr[:0]
buf = append(buf, '"')
buf = self.AppendTo(buf)
buf = append(buf, '"')
return buf, nil
}
/*
Implement `json.Unmarshaler`. If the input is empty or represents JSON `null`,
zeroes the receiver. Otherwise parses a JSON string, using the same algorithm
as `.Parse`.
*/
func (self *NullDate) UnmarshalJSON(src []byte) error {
if isJsonEmpty(src) {
self.Zero()
return nil
}
if isJsonStr(src) {
return self.UnmarshalText(cutJsonStr(src))
}
return errJsonString(src, self)
}
// Implement `driver.Valuer`, using `.Get`.
func (self NullDate) Value() (driver.Value, error) {
return self.Get(), nil
}
/*
Implement `sql.Scanner`, converting an arbitrary input to `gt.NullDate` and
modifying the receiver. Acceptable inputs:
* `nil` -> use `.Zero`
* `string` -> use `.Parse`
* `[]byte` -> use `.UnmarshalText`
* `time.Time` -> use `.SetTime`
* `*time.Time` -> use `.Zero` or `.SetTime`
* `gt.NullTime` -> use `.SetTime`
* `gt.NullDate` -> assign
* `gt.Getter` -> scan underlying value
*/
func (self *NullDate) Scan(src any) error {
switch src := src.(type) {
case nil:
self.Zero()
return nil
case string:
return self.Parse(src)
case []byte:
return self.UnmarshalText(src)
case time.Time:
self.SetTime(src)
return nil
case *time.Time:
if src == nil {
self.Zero()
} else {
self.SetTime(*src)
}
return nil
case NullTime:
self.SetTime(src.Time())
return nil
case NullDate:
*self = src
return nil
default:
val, ok := get(src)
if ok {
return self.Scan(val)
}
return errScanType(self, src)
}
}
// Implement `fmt.GoStringer`, returning valid Go code that constructs this value.
func (self NullDate) GoString() string {
year, month, day := self.Date()
return fmt.Sprintf(`gt.NullDateFrom(%v, %v, %v)`, year, int(month), day)
}
/*
If the input is zero, zeroes the receiver. Otherwise uses `time.Time.Date` and
assigns the resulting year, month, day to the receiver, ignoring smaller
constituents such as hour.
*/
func (self *NullDate) SetTime(src time.Time) {
// Note: `time.Time.Date()` "normalizes" zeros into 1 even when `.IsZero()`.
if src.IsZero() {
self.Zero()
} else {
*self = NullDateFrom(src.Date())
}
}
// Same as `time.Time.Date`. Returns a tuple of the underlying year, month, day.
func (self NullDate) Date() (year int, month time.Month, day int) {
return self.Year, self.Month, self.Day
}
// Converts to `gt.NullTime` with `T00:00:00` in the provided timezone.
func (self NullDate) NullTimeIn(loc *time.Location) NullTime {
return NullTime(time.Date(self.Year, self.Month, self.Day, 0, 0, 0, 0, loc))
}
// Converts to `gt.NullTime` with `T00:00:00` in UTC.
func (self NullDate) NullTimeUTC() NullTime {
return self.NullTimeIn(time.UTC)
}
// Converts to `time.Time` with `T00:00:00` in the provided timezone.
func (self NullDate) TimeIn(loc *time.Location) time.Time {
return self.NullTimeIn(loc).Time()
}
// Converts to `time.Time` with `T00:00:00` in UTC.
func (self NullDate) TimeUTC() time.Time {
return self.NullTimeUTC().Time()
}
/*
Similar to `time.Time.AddDate`. Returns a modified version of the current value,
with the year, month, day deltas added to the corresponding fields. The deltas
may be negative. Note that `time.Time` and all time-related types in this
package have a convenient `.Date` method that returns this tuple. The
calculations are performed for the UTC timezone.
As a special case, because the zero value is considered null, calling this on a
zero date ALWAYS returns the same zero date. This matches general SQL semantics
of operations involving nulls. Note that the equivalent of zero TIME is not
`gt.NullDateFrom(0, 0, 0)`, but rather `gt.NullDateFrom(1, 1, 1)`.
*/
func (self NullDate) AddDate(years int, months int, days int) NullDate {
if self.IsZero() {
return self
}
return NullDateFrom(self.NullTimeUTC().AddDate(years, months, days).Date())
}