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