-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.go
More file actions
61 lines (51 loc) · 1.25 KB
/
time.go
File metadata and controls
61 lines (51 loc) · 1.25 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
// EDIT(begin): custom time marshaler
package json
import (
"github.com/sfcompute/nodes-go/internal/encoding/json/shims"
"reflect"
"time"
)
type TimeMarshaler interface {
MarshalJSONWithTimeLayout(string) []byte
}
func TimeLayout(fmt string) string {
switch fmt {
case "", "date-time":
return time.RFC3339
case "date":
return time.DateOnly
default:
return fmt
}
}
var timeType = shims.TypeFor[time.Time]()
func newTimeEncoder() encoderFunc {
return func(e *encodeState, v reflect.Value, opts encOpts) {
t := v.Interface().(time.Time)
fmtted := t.Format(TimeLayout(opts.timefmt))
stringEncoder(e, reflect.ValueOf(fmtted), opts)
}
}
// Uses continuation passing style, to add the timefmt option to k
func continueWithTimeFmt(timefmt string, k encoderFunc) encoderFunc {
return func(e *encodeState, v reflect.Value, opts encOpts) {
opts.timefmt = timefmt
k(e, v, opts)
}
}
func timeMarshalEncoder(e *encodeState, v reflect.Value, opts encOpts) bool {
tm, ok := v.Interface().(TimeMarshaler)
if !ok {
return false
}
b := tm.MarshalJSONWithTimeLayout(opts.timefmt)
if b != nil {
e.Grow(len(b))
out := e.AvailableBuffer()
out, _ = appendCompact(out, b, opts.escapeHTML)
e.Buffer.Write(out)
return true
}
return false
}
// EDIT(end)