2 Copyright 2016 The Kubernetes Authors.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
23 "github.com/google/gofuzz"
26 const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
28 // MicroTime is version of Time with microsecond level precision.
30 // +protobuf.options.marshal=false
31 // +protobuf.as=Timestamp
32 // +protobuf.options.(gogoproto.goproto_stringer)=false
33 type MicroTime struct {
34 time.Time `protobuf:"-"`
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) {
44 // String returns the representation of the time.
45 func (t MicroTime) String() string {
46 return t.Time.String()
49 // NewMicroTime returns a wrapped instance of the provided time
50 func NewMicroTime(time time.Time) MicroTime {
51 return MicroTime{time}
54 // DateMicro returns the MicroTime corresponding to the supplied parameters
55 // by wrapping time.Date.
56 func DateMicro(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) MicroTime {
57 return MicroTime{time.Date(year, month, day, hour, min, sec, nsec, loc)}
60 // NowMicro returns the current local time.
61 func NowMicro() MicroTime {
62 return MicroTime{time.Now()}
65 // IsZero returns true if the value is nil or time is zero.
66 func (t *MicroTime) IsZero() bool {
70 return t.Time.IsZero()
73 // Before reports whether the time instant t is before u.
74 func (t *MicroTime) Before(u *MicroTime) bool {
75 return t.Time.Before(u.Time)
78 // Equal reports whether the time instant t is equal to u.
79 func (t *MicroTime) Equal(u *MicroTime) bool {
80 return t.Time.Equal(u.Time)
83 // BeforeTime reports whether the time instant t is before second-lever precision u.
84 func (t *MicroTime) BeforeTime(u *Time) bool {
85 return t.Time.Before(u.Time)
88 // EqualTime reports whether the time instant t is equal to second-lever precision u.
89 func (t *MicroTime) EqualTime(u *Time) bool {
90 return t.Time.Equal(u.Time)
93 // UnixMicro returns the local time corresponding to the given Unix time
94 // by wrapping time.Unix.
95 func UnixMicro(sec int64, nsec int64) MicroTime {
96 return MicroTime{time.Unix(sec, nsec)}
99 // UnmarshalJSON implements the json.Unmarshaller interface.
100 func (t *MicroTime) UnmarshalJSON(b []byte) error {
101 if len(b) == 4 && string(b) == "null" {
107 err := json.Unmarshal(b, &str)
112 pt, err := time.Parse(RFC3339Micro, str)
121 // UnmarshalQueryParameter converts from a URL query parameter value to an object
122 func (t *MicroTime) UnmarshalQueryParameter(str string) error {
127 // Tolerate requests from older clients that used JSON serialization to build query params
128 if len(str) == 4 && str == "null" {
133 pt, err := time.Parse(RFC3339Micro, str)
142 // MarshalJSON implements the json.Marshaler interface.
143 func (t MicroTime) MarshalJSON() ([]byte, error) {
145 // Encode unset/nil objects as JSON's "null".
146 return []byte("null"), nil
149 return json.Marshal(t.UTC().Format(RFC3339Micro))
152 // OpenAPISchemaType is used by the kube-openapi generator when constructing
153 // the OpenAPI spec of this type.
155 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
156 func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} }
158 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
159 // the OpenAPI spec of this type.
160 func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" }
162 // MarshalQueryParameter converts to a URL query parameter value
163 func (t MicroTime) MarshalQueryParameter() (string, error) {
165 // Encode unset/nil objects as an empty string
169 return t.UTC().Format(RFC3339Micro), nil
172 // Fuzz satisfies fuzz.Interface.
173 func (t *MicroTime) Fuzz(c fuzz.Continue) {
177 // Allow for about 1000 years of randomness. Accurate to a tenth of
178 // micro second. Leave off nanoseconds because JSON doesn't
179 // represent them so they can't round-trip properly.
180 t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 1000*c.Rand.Int63n(1000000))
183 var _ fuzz.Interface = &MicroTime{}