-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
337 lines (281 loc) · 7.41 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
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package dirsyn
/*
time.go implements all temporal syntaxes and matching rules -- namely
those for Generalized Time and the (deprecated) UTC Time.
*/
import "time"
/*
GeneralizedTime aliases an instance of [time.Time] to implement [§ 3.3.13
of RFC 4517]:
GeneralizedTime = century year month day hour
[ minute [ second / leap-second ] ]
[ fraction ]
g-time-zone
century = 2(%x30-39) ; "00" to "99"
year = 2(%x30-39) ; "00" to "99"
month = ( %x30 %x31-39 ) ; "01" (January) to "09"
/ ( %x31 %x30-32 ) ; "10" to "12"
day = ( %x30 %x31-39 ) ; "01" to "09"
/ ( %x31-32 %x30-39 ) ; "10" to "29"
/ ( %x33 %x30-31 ) ; "30" to "31"
hour = ( %x30-31 %x30-39 ) / ( %x32 %x30-33 ) ; "00" to "23"
minute = %x30-35 %x30-39 ; "00" to "59"
second = ( %x30-35 %x30-39 ) ; "00" to "59"
leap-second = ( %x36 %x30 ) ; "60"
fraction = ( DOT / COMMA ) 1*(%x30-39)
g-time-zone = %x5A ; "Z"
/ g-differential
g-differential = ( MINUS / PLUS ) hour [ minute ]
MINUS = %x2D ; minus sign ("-")
[§ 3.3.13 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.13
*/
type GeneralizedTime time.Time
/*
GeneralizedTime returns an instance of [GeneralizedTime] alongside an error.
*/
func (r RFC4517) GeneralizedTime(x any) (gt GeneralizedTime, err error) {
gt, err = marshalGenTime(x)
return
}
func generalizedTime(x any) (result Boolean) {
_, err := marshalGenTime(x)
result.Set(err == nil)
return
}
func marshalGenTime(x any) (gt GeneralizedTime, err error) {
var (
format string = `20060102150405` // base format
diff string = `-0700`
base string
raw string
)
if raw, err = assertString(x, 15, "Generalized Time"); err != nil {
return
}
raw = chopZulu(raw)
// If we've got nothing left, must be zulu
// without any fractional or differential
// components
if base = raw[14:]; len(base) == 0 {
var _gt time.Time
if _gt, err = time.Parse(format, raw); err == nil {
gt = GeneralizedTime(_gt)
}
return
}
// Handle fractional component (up to six (6) digits)
if format, err = genTimeFracDiffFormat(raw, base, diff, format); err != nil {
return
}
var _gt time.Time
if _gt, err = time.Parse(format, raw); err == nil {
gt = GeneralizedTime(_gt)
}
return
}
// Handle generalizedTime fractional component (up to six (6) digits)
func genTimeFracDiffFormat(raw, base, diff, format string) (string, error) {
var err error
if base[0] == '.' || base[0] == ',' {
format += string(".")
for fidx, ch := range base[1:] {
if fidx > 6 {
err = errorTxt(`Fraction exceeds Generalized Time fractional limit`)
} else if isDigit(ch) {
format += `0`
continue
}
break
}
}
// Handle differential time, or bail out if not
// already known to be zulu.
if raw[len(raw)-5] == '+' || raw[len(raw)-5] == '-' {
format += diff
}
return format, err
}
/*
String returns the string representation of the receiver instance.
*/
func (r GeneralizedTime) String() string {
return time.Time(r).Format(`20060102150405`) + `Z`
}
/*
Cast unwraps and returns the underlying instance of [time.Time].
*/
func (r GeneralizedTime) Cast() time.Time {
return time.Time(r)
}
/*
Deprecated: UTCTime implements [§ 3.3.34 of RFC 4517].
UTCTime = year month day hour minute [ second ] [ u-time-zone ]
u-time-zone = %x5A ; "Z"
/ u-differential
u-differential = ( MINUS / PLUS ) hour minute
Use instances of [GeneralizedTime] instead.
[§ 3.3.34 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-3.3.34
*/
type UTCTime time.Time
/*
String returns the string representation of the receiver instance.
*/
func (r UTCTime) String() string {
return time.Time(r).Format(`0601021504`) + `Z`
}
/*
Cast unwraps and returns the underlying instance of [time.Time].
*/
func (r UTCTime) Cast() time.Time {
return time.Time(r)
}
/*
Deprecated: UTCTime is intended for historical support only; use [GeneralizedTime]
instead.
UTCTime returns an error following an analysis of x in the context of a
(deprecated) UTC Time value.
*/
func (r RFC4517) UTCTime(x any) (utc UTCTime, err error) {
utc, err = marshalUTCTime(x)
return
}
func uTCTime(x any) (result Boolean) {
_, err := marshalUTCTime(x)
result.Set(err == nil)
return
}
func marshalUTCTime(x any) (utc UTCTime, err error) {
var (
format string = `0601021504` // base format
sec string = `05`
diff string = `-0700`
raw string
)
switch tv := x.(type) {
case string:
raw = chopZulu(tv)
if len(raw) < 10 {
err = errorBadLength(`UTC Time`, len(tv))
break
}
utc, err = uTCHandler(raw, sec, diff, format)
default:
err = errorBadType(`UTC Time`)
}
return
}
func chopZulu(raw string) string {
if zulu := raw[len(raw)-1] == 'Z'; zulu {
raw = raw[:len(raw)-1]
}
return raw
}
func uTCHandler(raw, sec, diff, format string) (utc UTCTime, err error) {
var _utc time.Time
switch len(raw) {
case 10:
// base time containing neither seconds
// nor a differential.
if _utc, err = time.Parse(format, raw); err == nil {
utc = UTCTime(_utc)
}
return
case 12:
// base time containing only seconds.
if _utc, err = time.Parse(format+sec, raw); err == nil {
utc = UTCTime(_utc)
}
return
}
format += sec
// Handle differential component
if raw[len(raw)-5] == '+' || raw[len(raw)-5] == '-' {
format += diff
}
if _utc, err = time.Parse(format, raw); err == nil {
utc = UTCTime(_utc)
}
return
}
/*
generalizedTimeMatch implements [§ 4.2.16 of RFC 4517].
OID: 2.5.13.27.
[§ 4.2.16 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.16
*/
func generalizedTimeMatch(a, b any) (result Boolean, err error) {
result, err = timeMatch(a, b, 0)
return
}
/*
generalizedTimeOrderingMatch implements [§ 4.2.17 of RFC 4517].
OID: 2.5.13.28.
[§ 4.2.17 of RFC 4517]: https://datatracker.ietf.org/doc/html/rfc4517#section-4.2.17
*/
func generalizedTimeOrderingMatch(a, b any) (result Boolean, err error) {
result, err = timeMatch(a, b, 2)
return
}
func timeMatch(rcv, assert any, typ int) (result Boolean, err error) {
var c time.Time
var utc bool
switch tv := rcv.(type) {
case GeneralizedTime:
c = tv.Cast().UTC()
case UTCTime:
c = tv.Cast().UTC()
utc = true
case string:
switch len(tv) {
case 15:
var gt GeneralizedTime
gt, err = marshalGenTime(tv)
c = gt.Cast().UTC()
case 10:
utc = true
var ut UTCTime
ut, err = marshalUTCTime(tv)
c = ut.Cast().UTC()
default:
err = errorTxt("invalid GeneralizedTime or UTCTime format")
}
default:
err = errorBadType("GeneralizedTime")
}
if err == nil {
var funk func(time.Time) bool
switch typ {
case 0:
funk = func(thyme time.Time) bool {
return c.Equal(thyme)
}
result.Set(compareTimes(assert, utc, funk))
case 2:
funk = func(thyme time.Time) bool {
return c.Before(thyme)
}
result.Set(compareTimes(assert, utc, funk))
default:
err = errorTxt("Invalid time format or value")
}
}
return
}
func compareTimes(assert any, utc bool, funk func(time.Time) bool) (result bool) {
switch tv := assert.(type) {
case GeneralizedTime:
result = funk(tv.Cast())
case UTCTime:
result = funk(tv.Cast())
case time.Time:
result = funk(tv)
default:
if utc {
d, err := marshalUTCTime(tv)
result = funk(d.Cast()) && err == nil
} else {
d, err := marshalGenTime(tv)
result = funk(d.Cast()) && err == nil
}
}
return
}