-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtime.go
More file actions
32 lines (26 loc) · 779 Bytes
/
time.go
File metadata and controls
32 lines (26 loc) · 779 Bytes
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
package jsonutil
import (
"encoding/json/jsontext"
"encoding/json/v2"
"time"
)
// TimeMarshalIntUnix is a custom marshaler for time.Time, marshaling them as integers representing unix time.
func TimeMarshalIntUnix(enc *jsontext.Encoder, t time.Time) error {
if t.IsZero() {
return enc.WriteToken(jsontext.Int(0))
}
return enc.WriteToken(jsontext.Int(int64(t.Unix())))
}
// TimeUnmarshalIntUnix is a custom unmarshaler for time.Time, unmarshaling them from integers and assuming they represent unix time.
func TimeUnmarshalIntUnix(dec *jsontext.Decoder, d *time.Time) error {
var seconds int64
if err := json.UnmarshalDecode(dec, &seconds); err != nil {
return err
}
if seconds == 0 {
*d = time.Time{}
} else {
*d = time.Unix(seconds, 0)
}
return nil
}