Debian Codegraph @

pkg/apis/meta/v1/time.go

raw
1 /*
2 Copyright 2014 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package v1
18
19 import (
20 "encoding/json"
21 "time"
22
23 cbor "k8s.io/apimachinery/pkg/runtime/serializer/cbor/direct"
24 )
25
26 // Time is a wrapper around time.Time which supports correct
27 // marshaling to YAML and JSON. Wrappers are provided for many
28 // of the factory methods that the time package offers.
29 //
30 // +protobuf.options.marshal=false
31 // +protobuf.as=Timestamp
32 // +protobuf.options.(gogoproto.goproto_stringer)=false
33 type Time struct {
34 time.Time `protobuf:"-"`
35 }
36
37 // DeepCopyInto creates a deep-copy of the Time value. The underlying time.Time
38 // type is effectively immutable in the time API, so it is safe to
39 // copy-by-assign, despite the presence of (unexported) Pointer fields.
40 func (t *Time) DeepCopyInto(out *Time) {
41 *out = *t
42 }
43
44 // NewTime returns a wrapped instance of the provided time
45 func NewTime(time time.Time) Time {
46 return Time{time}
47 }
48
49 // Date returns the Time corresponding to the supplied parameters
50 // by wrapping time.Date.
51 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
52 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
53 }
54
55 // Now returns the current local time.
56 func Now() Time {
57 return Time{time.Now()}
58 }
59
60 // IsZero returns true if the value is nil or time is zero.
61 func (t *Time) IsZero() bool {
62 if t == nil {
63 return true
64 }
65 return t.Time.IsZero()
66 }
67
68 // Before reports whether the time instant t is before u.
69 func (t *Time) Before(u *Time) bool {
70 if t != nil && u != nil {
71 return t.Time.Before(u.Time)
72 }
73 return false
74 }
75
76 // Equal reports whether the time instant t is equal to u.
77 func (t *Time) Equal(u *Time) bool {
78 if t == nil && u == nil {
79 return true
80 }
81 if t != nil && u != nil {
82 return t.Time.Equal(u.Time)
83 }
84 return false
85 }
86
87 // Unix returns the local time corresponding to the given Unix time
88 // by wrapping time.Unix.
89 func Unix(sec int64, nsec int64) Time {
90 return Time{time.Unix(sec, nsec)}
91 }
92
93 // Rfc3339Copy returns a copy of the Time at second-level precision.
94 func (t Time) Rfc3339Copy() Time {
95 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
96 return Time{copied}
97 }
98
99 // UnmarshalJSON implements the json.Unmarshaller interface.
100 func (t *Time) UnmarshalJSON(b []byte) error {
101 if len(b) == 4 && string(b) == "null" {
102 t.Time = time.Time{}
103 return nil
104 }
105
106 var str string
107 err := json.Unmarshal(b, &str)
108 if err != nil {
109 return err
110 }
111
112 pt, err := time.Parse(time.RFC3339, str)
113 if err != nil {
114 return err
115 }
116
117 t.Time = pt.Local()
118 return nil
119 }
120
121 func (t *Time) UnmarshalCBOR(b []byte) error {
122 var s *string
123 if err := cbor.Unmarshal(b, &s); err != nil {
124 return err
125 }
126 if s == nil {
127 t.Time = time.Time{}
128 return nil
129 }
130
131 parsed, err := time.Parse(time.RFC3339, *s)
132 if err != nil {
133 return err
134 }
135
136 t.Time = parsed.Local()
137 return nil
138 }
139
140 // UnmarshalQueryParameter converts from a URL query parameter value to an object
141 func (t *Time) UnmarshalQueryParameter(str string) error {
142 if len(str) == 0 {
143 t.Time = time.Time{}
144 return nil
145 }
146 // Tolerate requests from older clients that used JSON serialization to build query params
147 if len(str) == 4 && str == "null" {
148 t.Time = time.Time{}
149 return nil
150 }
151
152 pt, err := time.Parse(time.RFC3339, str)
153 if err != nil {
154 return err
155 }
156
157 t.Time = pt.Local()
158 return nil
159 }
160
161 // MarshalJSON implements the json.Marshaler interface.
162 func (t Time) MarshalJSON() ([]byte, error) {
163 if t.IsZero() {
164 // Encode unset/nil objects as JSON's "null".
165 return []byte("null"), nil
166 }
167 buf := make([]byte, 0, len(time.RFC3339)+2)
168 buf = append(buf, '"')
169 // time cannot contain non escapable JSON characters
170 buf = t.UTC().AppendFormat(buf, time.RFC3339)
171 buf = append(buf, '"')
172 return buf, nil
173 }
174
175 func (t Time) MarshalCBOR() ([]byte, error) {
176 if t.IsZero() {
177 return cbor.Marshal(nil)
178 }
179
180 return cbor.Marshal(t.UTC().Format(time.RFC3339))
181 }
182
183 // ToUnstructured implements the value.UnstructuredConverter interface.
184 func (t Time) ToUnstructured() interface{} {
185 if t.IsZero() {
186 return nil
187 }
188 buf := make([]byte, 0, len(time.RFC3339))
189 buf = t.UTC().AppendFormat(buf, time.RFC3339)
190 return string(buf)
191 }
192
193 // OpenAPISchemaType is used by the kube-openapi generator when constructing
194 // the OpenAPI spec of this type.
195 //
196 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
197 func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
198
199 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
200 // the OpenAPI spec of this type.
201 func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
202
203 // MarshalQueryParameter converts to a URL query parameter value
204 func (t Time) MarshalQueryParameter() (string, error) {
205 if t.IsZero() {
206 // Encode unset/nil objects as an empty string
207 return "", nil
208 }
209
210 return t.UTC().Format(time.RFC3339), nil
211 }