-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamp.go
122 lines (104 loc) · 2.59 KB
/
timestamp.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
package timestamp
import (
"encoding/json"
"errors"
"strconv"
"time"
"github.com/valyala/fastjson"
)
// ToTimestamp generates a millisecond timestamp from the time object.
func ToTimestamp(t time.Time) Timestamp {
return Timestamp{
TimeUnixMs: ToUnixMs(t),
}
}
// ToUnixMs converts a time to unix ms.
func ToUnixMs(t time.Time) uint64 {
return uint64(t.UnixNano() / 1000000)
}
// ToTime generates a time object from a millisecond timestamp.
func ToTime(t uint64) time.Time {
return time.Unix(0, int64(t)*1000000).UTC()
}
// ToTime converts the Timestamp to a time.Time
func (t *Timestamp) ToTime() time.Time {
if t.GetTimeUnixMs() == 0 {
return time.Time{}
}
return ToTime(t.GetTimeUnixMs())
}
// Now returns a timestamp for now.
func Now() Timestamp {
return ToTimestamp(time.Now().UTC())
}
// Clone copies the timestamp.
func (t *Timestamp) Clone() *Timestamp {
if t == nil {
return nil
}
return &Timestamp{TimeUnixMs: t.TimeUnixMs}
}
// Equals checks if the timestamp equals another timestamp.
func (t *Timestamp) Equals(ot *Timestamp) bool {
return t.GetTimeUnixMs() == ot.GetTimeUnixMs()
}
// Validate checks the timestamp.
func (t *Timestamp) Validate(allowEmpty bool) error {
if !allowEmpty && t.GetEmpty() {
return ErrEmptyTimestamp
}
return nil
}
// GetEmpty checks if the timestamp is empty.
func (t *Timestamp) GetEmpty() bool {
return t.GetTimeUnixMs() == 0
}
// UnmarshalJSON unmarshals json.
// Supports string (unix milliseconds large value or RFC3339 timestamp), number (unix milliseconds)
func (t *Timestamp) UnmarshalJSON(data []byte) error {
t.Reset()
var p fastjson.Parser
v, err := p.ParseBytes(data)
if err != nil {
return err
}
if v.Type() == fastjson.TypeNumber {
ms, err := v.Uint64()
if err != nil {
return err
}
t.TimeUnixMs = ms
return nil
}
if v.Type() == fastjson.TypeObject {
if v.Exists("timeUnixMs") {
t.TimeUnixMs = v.GetUint64("timeUnixMs")
}
return nil
}
if v.Type() == fastjson.TypeString {
str := string(v.GetStringBytes())
// try to parse as RFC3339
tt, err := time.Parse(time.RFC3339, str)
if err == nil {
t.TimeUnixMs = ToUnixMs(tt)
return nil
}
timeMs, err := strconv.ParseUint(str, 10, 64)
if err == nil {
t.TimeUnixMs = timeMs
return nil
}
return errors.New("cannot parse timestamp string")
}
return nil
}
// MarshalJSON marshals to a JSON RFC3339 timestamp.
func (t *Timestamp) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(t.ToTime().Format(time.RFC3339))), nil
}
// _ is a type assertion
var (
_ json.Unmarshaler = ((*Timestamp)(nil))
_ json.Marshaler = ((*Timestamp)(nil))
)