-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
73 lines (63 loc) · 1.81 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
package rest
import (
"context"
"time"
"github.com/KarpelesLab/pjson"
)
type Time struct {
time.Time
}
type timestampInternal struct {
Unix int64 `json:"unix"` // 1597242491
Usec int64 `json:"us"` // 747497
TZ string `json:"tz,omitempty"` // Asia/Tokyo
ISO string `json:"iso,omitempty"` // "2020-08-12 23:28:11.747497"
Full int64 `json:"full,omitempty,string"` // "1597242491747497"
UnixMS int64 `json:"unixms,omitempty,string"` // "1597242491747"
}
func (u *Time) UnmarshalJSON(data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
var sd timestampInternal
err := pjson.Unmarshal(data, &sd)
if err != nil {
return err
}
u.Time = time.Unix(sd.Unix, sd.Usec*1000) // *1000 means µs → ns
return nil
}
func (u Time) MarshalJSON() ([]byte, error) {
var sd timestampInternal
sd.Unix = u.Unix()
sd.Usec = int64(u.Nanosecond() / 1000)
sd.TZ = u.Location().String()
sd.ISO = u.UTC().Format("2006-01-02 15:04:05")
sd.Full = u.UnixMicro()
sd.UnixMS = u.UnixMilli()
return pjson.Marshal(sd)
}
func (u *Time) UnmarshalContextJSON(ctx context.Context, data []byte) error {
// Ignore null, like in the main JSON package.
if string(data) == "null" {
return nil
}
var sd timestampInternal
err := pjson.UnmarshalContext(ctx, data, &sd)
if err != nil {
return err
}
u.Time = time.Unix(sd.Unix, sd.Usec*1000) // *1000 means µs → ns
return nil
}
func (u Time) MarshalContextJSON(ctx context.Context) ([]byte, error) {
var sd timestampInternal
sd.Unix = u.Unix()
sd.Usec = int64(u.Nanosecond() / 1000)
sd.TZ = u.Location().String()
sd.ISO = u.UTC().Format("2006-01-02 15:04:05")
sd.Full = u.UnixMicro()
sd.UnixMS = u.UnixMilli()
return pjson.MarshalContext(ctx, sd)
}