Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 23 | fuzz "github.com/google/gofuzz" |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 { |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 70 | if t != nil && u != nil { |
| 71 | return t.Time.Before(u.Time) |
| 72 | } |
| 73 | return false |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | // UnmarshalQueryParameter converts from a URL query parameter value to an object |
| 122 | func (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. |
| 143 | func (t Time) MarshalJSON() ([]byte, error) { |
| 144 | if t.IsZero() { |
| 145 | // Encode unset/nil objects as JSON's "null". |
| 146 | return []byte("null"), nil |
| 147 | } |
David Bainbridge | 8697152 | 2019-09-26 22:09:39 +0000 | [diff] [blame] | 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 |
Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // OpenAPISchemaType is used by the kube-openapi generator when constructing |
| 157 | // the OpenAPI spec of this type. |
| 158 | // |
| 159 | // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators |
| 160 | func (_ Time) OpenAPISchemaType() []string { return []string{"string"} } |
| 161 | |
| 162 | // OpenAPISchemaFormat is used by the kube-openapi generator when constructing |
| 163 | // the OpenAPI spec of this type. |
| 164 | func (_ Time) OpenAPISchemaFormat() string { return "date-time" } |
| 165 | |
| 166 | // MarshalQueryParameter converts to a URL query parameter value |
| 167 | func (t Time) MarshalQueryParameter() (string, error) { |
| 168 | if t.IsZero() { |
| 169 | // Encode unset/nil objects as an empty string |
| 170 | return "", nil |
| 171 | } |
| 172 | |
| 173 | return t.UTC().Format(time.RFC3339), nil |
| 174 | } |
| 175 | |
| 176 | // Fuzz satisfies fuzz.Interface. |
| 177 | func (t *Time) Fuzz(c fuzz.Continue) { |
| 178 | if t == nil { |
| 179 | return |
| 180 | } |
| 181 | // Allow for about 1000 years of randomness. Leave off nanoseconds |
| 182 | // because JSON doesn't represent them so they can't round-trip |
| 183 | // properly. |
| 184 | t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0) |
| 185 | } |
| 186 | |
| 187 | var _ fuzz.Interface = &Time{} |