...
1
16
17 package v1
18
19 import (
20 "encoding/json"
21 "time"
22 )
23
24 const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
25
26
27
28
29
30
31 type MicroTime struct {
32 time.Time `protobuf:"-"`
33 }
34
35
36
37
38 func (t *MicroTime) DeepCopyInto(out *MicroTime) {
39 *out = *t
40 }
41
42
43 func NewMicroTime(time time.Time) MicroTime {
44 return MicroTime{time}
45 }
46
47
48
49 func DateMicro(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) MicroTime {
50 return MicroTime{time.Date(year, month, day, hour, min, sec, nsec, loc)}
51 }
52
53
54 func NowMicro() MicroTime {
55 return MicroTime{time.Now()}
56 }
57
58
59 func (t *MicroTime) IsZero() bool {
60 if t == nil {
61 return true
62 }
63 return t.Time.IsZero()
64 }
65
66
67 func (t *MicroTime) Before(u *MicroTime) bool {
68 if t != nil && u != nil {
69 return t.Time.Before(u.Time)
70 }
71 return false
72 }
73
74
75 func (t *MicroTime) Equal(u *MicroTime) bool {
76 if t == nil && u == nil {
77 return true
78 }
79 if t != nil && u != nil {
80 return t.Time.Equal(u.Time)
81 }
82 return false
83 }
84
85
86 func (t *MicroTime) BeforeTime(u *Time) bool {
87 if t != nil && u != nil {
88 return t.Time.Before(u.Time)
89 }
90 return false
91 }
92
93
94 func (t *MicroTime) EqualTime(u *Time) bool {
95 if t == nil && u == nil {
96 return true
97 }
98 if t != nil && u != nil {
99 return t.Time.Equal(u.Time)
100 }
101 return false
102 }
103
104
105
106 func UnixMicro(sec int64, nsec int64) MicroTime {
107 return MicroTime{time.Unix(sec, nsec)}
108 }
109
110
111 func (t *MicroTime) UnmarshalJSON(b []byte) error {
112 if len(b) == 4 && string(b) == "null" {
113 t.Time = time.Time{}
114 return nil
115 }
116
117 var str string
118 err := json.Unmarshal(b, &str)
119 if err != nil {
120 return err
121 }
122
123 pt, err := time.Parse(RFC3339Micro, str)
124 if err != nil {
125 return err
126 }
127
128 t.Time = pt.Local()
129 return nil
130 }
131
132
133 func (t *MicroTime) UnmarshalQueryParameter(str string) error {
134 if len(str) == 0 {
135 t.Time = time.Time{}
136 return nil
137 }
138
139 if len(str) == 4 && str == "null" {
140 t.Time = time.Time{}
141 return nil
142 }
143
144 pt, err := time.Parse(RFC3339Micro, str)
145 if err != nil {
146 return err
147 }
148
149 t.Time = pt.Local()
150 return nil
151 }
152
153
154 func (t MicroTime) MarshalJSON() ([]byte, error) {
155 if t.IsZero() {
156
157 return []byte("null"), nil
158 }
159
160 return json.Marshal(t.UTC().Format(RFC3339Micro))
161 }
162
163
164
165
166
167 func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} }
168
169
170
171 func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" }
172
173
174 func (t MicroTime) MarshalQueryParameter() (string, error) {
175 if t.IsZero() {
176
177 return "", nil
178 }
179
180 return t.UTC().Format(RFC3339Micro), nil
181 }
182
View as plain text