2 Copyright 2014 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 // 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.
30 // +protobuf.options.marshal=false
31 // +protobuf.as=Timestamp
32 // +protobuf.options.(gogoproto.goproto_stringer)=false
34 time.Time `protobuf:"-"`
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) {
44 // String returns the representation of the time.
45 func (t Time) String() string {
46 return t.Time.String()
49 // NewTime returns a wrapped instance of the provided time
50 func NewTime(time time.Time) Time {
54 // Date returns the Time corresponding to the supplied parameters
55 // by wrapping time.Date.
56 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
57 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
60 // Now returns the current local time.
62 return Time{time.Now()}
65 // IsZero returns true if the value is nil or time is zero.
66 func (t *Time) IsZero() bool {
70 return t.Time.IsZero()
73 // Before reports whether the time instant t is before u.
74 func (t *Time) Before(u *Time) bool {
75 return t.Time.Before(u.Time)
78 // Equal reports whether the time instant t is equal to u.
79 func (t *Time) Equal(u *Time) bool {
80 if t == nil && u == nil {
83 if t != nil && u != nil {
84 return t.Time.Equal(u.Time)
89 // Unix returns the local time corresponding to the given Unix time
90 // by wrapping time.Unix.
91 func Unix(sec int64, nsec int64) Time {
92 return Time{time.Unix(sec, nsec)}
95 // Rfc3339Copy returns a copy of the Time at second-level precision.
96 func (t Time) Rfc3339Copy() Time {
97 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
101 // UnmarshalJSON implements the json.Unmarshaller interface.
102 func (t *Time) UnmarshalJSON(b []byte) error {
103 if len(b) == 4 && string(b) == "null" {
109 err := json.Unmarshal(b, &str)
114 pt, err := time.Parse(time.RFC3339, str)
123 // UnmarshalQueryParameter converts from a URL query parameter value to an object
124 func (t *Time) UnmarshalQueryParameter(str string) error {
129 // Tolerate requests from older clients that used JSON serialization to build query params
130 if len(str) == 4 && str == "null" {
135 pt, err := time.Parse(time.RFC3339, str)
144 // MarshalJSON implements the json.Marshaler interface.
145 func (t Time) MarshalJSON() ([]byte, error) {
147 // Encode unset/nil objects as JSON's "null".
148 return []byte("null"), nil
151 return json.Marshal(t.UTC().Format(time.RFC3339))
154 // OpenAPISchemaType is used by the kube-openapi generator when constructing
155 // the OpenAPI spec of this type.
157 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
158 func (_ Time) OpenAPISchemaType() []string { return []string{"string"} }
160 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
161 // the OpenAPI spec of this type.
162 func (_ Time) OpenAPISchemaFormat() string { return "date-time" }
164 // MarshalQueryParameter converts to a URL query parameter value
165 func (t Time) MarshalQueryParameter() (string, error) {
167 // Encode unset/nil objects as an empty string
171 return t.UTC().Format(time.RFC3339), nil
174 // Fuzz satisfies fuzz.Interface.
175 func (t *Time) Fuzz(c fuzz.Continue) {
179 // Allow for about 1000 years of randomness. Leave off nanoseconds
180 // because JSON doesn't represent them so they can't round-trip
182 t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0)
185 var _ fuzz.Interface = &Time{}