| 1 |
/* |
| 2 |
Copyright 2016 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 |
const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00" |
| 27 |
|
| 28 |
// MicroTime is version of Time with microsecond level precision. |
| 29 |
// |
| 30 |
// +protobuf.options.marshal=false |
| 31 |
// +protobuf.as=Timestamp |
| 32 |
// +protobuf.options.(gogoproto.goproto_stringer)=false |
| 33 |
type MicroTime struct { |
| 34 |
time.Time `protobuf:"-"` |
| 35 |
} |
| 36 |
|
| 37 |
// DeepCopy returns a deep-copy of the MicroTime 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 *MicroTime) DeepCopyInto(out *MicroTime) { |
| 41 |
*out = *t |
| 42 |
} |
| 43 |
|
| 44 |
// NewMicroTime returns a wrapped instance of the provided time |
| 45 |
func NewMicroTime(time time.Time) MicroTime { |
| 46 |
return MicroTime{time} |
| 47 |
} |
| 48 |
|
| 49 |
// DateMicro returns the MicroTime corresponding to the supplied parameters |
| 50 |
// by wrapping time.Date. |
| 51 |
func DateMicro(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) MicroTime { |
| 52 |
return MicroTime{time.Date(year, month, day, hour, min, sec, nsec, loc)} |
| 53 |
} |
| 54 |
|
| 55 |
// NowMicro returns the current local time. |
| 56 |
func NowMicro() MicroTime { |
| 57 |
return MicroTime{time.Now()} |
| 58 |
} |
| 59 |
|
| 60 |
// IsZero returns true if the value is nil or time is zero. |
| 61 |
func (t *MicroTime) 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 *MicroTime) Before(u *MicroTime) 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 *MicroTime) Equal(u *MicroTime) 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 |
// BeforeTime reports whether the time instant t is before second-lever precision u. |
| 88 |
func (t *MicroTime) BeforeTime(u *Time) bool { |
| 89 |
if t != nil && u != nil { |
| 90 |
return t.Time.Before(u.Time) |
| 91 |
} |
| 92 |
return false |
| 93 |
} |
| 94 |
|
| 95 |
// EqualTime reports whether the time instant t is equal to second-lever precision u. |
| 96 |
func (t *MicroTime) EqualTime(u *Time) bool { |
| 97 |
if t == nil && u == nil { |
| 98 |
return true |
| 99 |
} |
| 100 |
if t != nil && u != nil { |
| 101 |
return t.Time.Equal(u.Time) |
| 102 |
} |
| 103 |
return false |
| 104 |
} |
| 105 |
|
| 106 |
// UnixMicro returns the local time corresponding to the given Unix time |
| 107 |
// by wrapping time.Unix. |
| 108 |
func UnixMicro(sec int64, nsec int64) MicroTime { |
| 109 |
return MicroTime{time.Unix(sec, nsec)} |
| 110 |
} |
| 111 |
|
| 112 |
// UnmarshalJSON implements the json.Unmarshaller interface. |
| 113 |
func (t *MicroTime) UnmarshalJSON(b []byte) error { |
| 114 |
if len(b) == 4 && string(b) == "null" { |
| 115 |
t.Time = time.Time{} |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
var str string |
| 120 |
err := json.Unmarshal(b, &str) |
| 121 |
if err != nil { |
| 122 |
return err |
| 123 |
} |
| 124 |
|
| 125 |
pt, err := time.Parse(RFC3339Micro, str) |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
|
| 130 |
t.Time = pt.Local() |
| 131 |
return nil |
| 132 |
} |
| 133 |
|
| 134 |
func (t *MicroTime) UnmarshalCBOR(b []byte) error { |
| 135 |
var s *string |
| 136 |
if err := cbor.Unmarshal(b, &s); err != nil { |
| 137 |
return err |
| 138 |
} |
| 139 |
if s == nil { |
| 140 |
t.Time = time.Time{} |
| 141 |
return nil |
| 142 |
} |
| 143 |
|
| 144 |
parsed, err := time.Parse(RFC3339Micro, *s) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
|
| 149 |
t.Time = parsed.Local() |
| 150 |
return nil |
| 151 |
} |
| 152 |
|
| 153 |
// UnmarshalQueryParameter converts from a URL query parameter value to an object |
| 154 |
func (t *MicroTime) UnmarshalQueryParameter(str string) error { |
| 155 |
if len(str) == 0 { |
| 156 |
t.Time = time.Time{} |
| 157 |
return nil |
| 158 |
} |
| 159 |
// Tolerate requests from older clients that used JSON serialization to build query params |
| 160 |
if len(str) == 4 && str == "null" { |
| 161 |
t.Time = time.Time{} |
| 162 |
return nil |
| 163 |
} |
| 164 |
|
| 165 |
pt, err := time.Parse(RFC3339Micro, str) |
| 166 |
if err != nil { |
| 167 |
return err |
| 168 |
} |
| 169 |
|
| 170 |
t.Time = pt.Local() |
| 171 |
return nil |
| 172 |
} |
| 173 |
|
| 174 |
// MarshalJSON implements the json.Marshaler interface. |
| 175 |
func (t MicroTime) MarshalJSON() ([]byte, error) { |
| 176 |
if t.IsZero() { |
| 177 |
// Encode unset/nil objects as JSON's "null". |
| 178 |
return []byte("null"), nil |
| 179 |
} |
| 180 |
|
| 181 |
return json.Marshal(t.UTC().Format(RFC3339Micro)) |
| 182 |
} |
| 183 |
|
| 184 |
func (t MicroTime) MarshalCBOR() ([]byte, error) { |
| 185 |
if t.IsZero() { |
| 186 |
return cbor.Marshal(nil) |
| 187 |
} |
| 188 |
return cbor.Marshal(t.UTC().Format(RFC3339Micro)) |
| 189 |
} |
| 190 |
|
| 191 |
// OpenAPISchemaType is used by the kube-openapi generator when constructing |
| 192 |
// the OpenAPI spec of this type. |
| 193 |
// |
| 194 |
// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators |
| 195 |
func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} } |
| 196 |
|
| 197 |
// OpenAPISchemaFormat is used by the kube-openapi generator when constructing |
| 198 |
// the OpenAPI spec of this type. |
| 199 |
func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" } |
| 200 |
|
| 201 |
// MarshalQueryParameter converts to a URL query parameter value |
| 202 |
func (t MicroTime) MarshalQueryParameter() (string, error) { |
| 203 |
if t.IsZero() { |
| 204 |
// Encode unset/nil objects as an empty string |
| 205 |
return "", nil |
| 206 |
} |
| 207 |
|
| 208 |
return t.UTC().Format(RFC3339Micro), nil |
| 209 |
} |