github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/time.go (about) 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 ) 23 24 // Time is a wrapper around time.Time which supports correct 25 // marshaling to YAML and JSON. Wrappers are provided for many 26 // of the factory methods that the time package offers. 27 // 28 // +protobuf.options.marshal=false 29 // +protobuf.as=Timestamp 30 // +protobuf.options.(gogoproto.goproto_stringer)=false 31 type Time struct { 32 time.Time `protobuf:"-"` 33 } 34 35 // DeepCopyInto creates a deep-copy of the Time value. The underlying time.Time 36 // type is effectively immutable in the time API, so it is safe to 37 // copy-by-assign, despite the presence of (unexported) Pointer fields. 38 func (t *Time) DeepCopyInto(out *Time) { 39 *out = *t 40 } 41 42 // NewTime returns a wrapped instance of the provided time 43 func NewTime(time time.Time) Time { 44 return Time{time} 45 } 46 47 // Date returns the Time corresponding to the supplied parameters 48 // by wrapping time.Date. 49 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { 50 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} 51 } 52 53 // Now returns the current local time. 54 func Now() Time { 55 return Time{time.Now()} 56 } 57 58 // IsZero returns true if the value is nil or time is zero. 59 func (t *Time) IsZero() bool { 60 if t == nil { 61 return true 62 } 63 return t.Time.IsZero() 64 } 65 66 // Before reports whether the time instant t is before u. 67 func (t *Time) Before(u *Time) bool { 68 if t != nil && u != nil { 69 return t.Time.Before(u.Time) 70 } 71 return false 72 } 73 74 // Equal reports whether the time instant t is equal to u. 75 func (t *Time) Equal(u *Time) bool { 76 if t == nil && u == nil { 77 return true 78 } 79 if t != nil && u != nil { 80 return t.Time.Equal(u.Time) 81 } 82 return false 83 } 84 85 // Unix returns the local time corresponding to the given Unix time 86 // by wrapping time.Unix. 87 func Unix(sec int64, nsec int64) Time { 88 return Time{time.Unix(sec, nsec)} 89 } 90 91 // Rfc3339Copy returns a copy of the Time at second-level precision. 92 func (t Time) Rfc3339Copy() Time { 93 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339)) 94 return Time{copied} 95 } 96 97 // UnmarshalJSON implements the json.Unmarshaller interface. 98 func (t *Time) UnmarshalJSON(b []byte) error { 99 if len(b) == 4 && string(b) == "null" { 100 t.Time = time.Time{} 101 return nil 102 } 103 104 var str string 105 err := json.Unmarshal(b, &str) 106 if err != nil { 107 return err 108 } 109 110 pt, err := time.Parse(time.RFC3339, str) 111 if err != nil { 112 return err 113 } 114 115 t.Time = pt.Local() 116 return nil 117 } 118 119 // UnmarshalQueryParameter converts from a URL query parameter value to an object 120 func (t *Time) UnmarshalQueryParameter(str string) error { 121 if len(str) == 0 { 122 t.Time = time.Time{} 123 return nil 124 } 125 // Tolerate requests from older clients that used JSON serialization to build query params 126 if len(str) == 4 && str == "null" { 127 t.Time = time.Time{} 128 return nil 129 } 130 131 pt, err := time.Parse(time.RFC3339, str) 132 if err != nil { 133 return err 134 } 135 136 t.Time = pt.Local() 137 return nil 138 } 139 140 // MarshalJSON implements the json.Marshaler interface. 141 func (t Time) MarshalJSON() ([]byte, error) { 142 if t.IsZero() { 143 // Encode unset/nil objects as JSON's "null". 144 return []byte("null"), nil 145 } 146 buf := make([]byte, 0, len(time.RFC3339)+2) 147 buf = append(buf, '"') 148 // time cannot contain non escapable JSON characters 149 buf = t.UTC().AppendFormat(buf, time.RFC3339) 150 buf = append(buf, '"') 151 return buf, nil 152 } 153 154 // ToUnstructured implements the value.UnstructuredConverter interface. 155 func (t Time) ToUnstructured() interface{} { 156 if t.IsZero() { 157 return nil 158 } 159 buf := make([]byte, 0, len(time.RFC3339)) 160 buf = t.UTC().AppendFormat(buf, time.RFC3339) 161 return string(buf) 162 } 163 164 // OpenAPISchemaType is used by the kube-openapi generator when constructing 165 // the OpenAPI spec of this type. 166 // 167 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators 168 func (_ Time) OpenAPISchemaType() []string { return []string{"string"} } 169 170 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing 171 // the OpenAPI spec of this type. 172 func (_ Time) OpenAPISchemaFormat() string { return "date-time" } 173 174 // MarshalQueryParameter converts to a URL query parameter value 175 func (t Time) MarshalQueryParameter() (string, error) { 176 if t.IsZero() { 177 // Encode unset/nil objects as an empty string 178 return "", nil 179 } 180 181 return t.UTC().Format(time.RFC3339), nil 182 }