blob: 4a1d89cfce85cd0d642fb47676b9768fa0b65eb4 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20 "encoding/json"
21 "time"
22
23 fuzz "github.com/google/gofuzz"
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
33type 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.
40func (t *Time) DeepCopyInto(out *Time) {
41 *out = *t
42}
43
44// NewTime returns a wrapped instance of the provided time
45func 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.
51func 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.
56func Now() Time {
57 return Time{time.Now()}
58}
59
60// IsZero returns true if the value is nil or time is zero.
61func (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.
69func (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.
77func (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.
89func 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.
94func (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.
100func (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// UnmarshalQueryParameter converts from a URL query parameter value to an object
122func (t *Time) UnmarshalQueryParameter(str string) error {
123 if len(str) == 0 {
124 t.Time = time.Time{}
125 return nil
126 }
127 // Tolerate requests from older clients that used JSON serialization to build query params
128 if len(str) == 4 && str == "null" {
129 t.Time = time.Time{}
130 return nil
131 }
132
133 pt, err := time.Parse(time.RFC3339, str)
134 if err != nil {
135 return err
136 }
137
138 t.Time = pt.Local()
139 return nil
140}
141
142// MarshalJSON implements the json.Marshaler interface.
143func (t Time) MarshalJSON() ([]byte, error) {
144 if t.IsZero() {
145 // Encode unset/nil objects as JSON's "null".
146 return []byte("null"), nil
147 }
148 buf := make([]byte, 0, len(time.RFC3339)+2)
149 buf = append(buf, '"')
150 // time cannot contain non escapable JSON characters
151 buf = t.UTC().AppendFormat(buf, time.RFC3339)
152 buf = append(buf, '"')
153 return buf, nil
154}
155
156// ToUnstructured implements the value.UnstructuredConverter interface.
157func (t Time) ToUnstructured() interface{} {
158 if t.IsZero() {
159 return nil
160 }
161 buf := make([]byte, 0, len(time.RFC3339))
162 buf = t.UTC().AppendFormat(buf, time.RFC3339)
163 return string(buf)
164}
165
166// OpenAPISchemaType is used by the kube-openapi generator when constructing
167// the OpenAPI spec of this type.
168//
169// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
170func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
171
172// OpenAPISchemaFormat is used by the kube-openapi generator when constructing
173// the OpenAPI spec of this type.
174func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
175
176// MarshalQueryParameter converts to a URL query parameter value
177func (t Time) MarshalQueryParameter() (string, error) {
178 if t.IsZero() {
179 // Encode unset/nil objects as an empty string
180 return "", nil
181 }
182
183 return t.UTC().Format(time.RFC3339), nil
184}
185
186// Fuzz satisfies fuzz.Interface.
187func (t *Time) Fuzz(c fuzz.Continue) {
188 if t == nil {
189 return
190 }
191 // Allow for about 1000 years of randomness. Leave off nanoseconds
192 // because JSON doesn't represent them so they can't round-trip
193 // properly.
194 t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0)
195}
196
197var _ fuzz.Interface = &Time{}