golang.org/x/build@v0.0.0-20240506185731-218518f32b70/kubernetes/api/time.go (about) 1 /* 2 Copyright 2014 The Kubernetes Authors All rights reserved. 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 api 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 type Time struct { 28 time.Time 29 } 30 31 // NewTime returns a wrapped instance of the provided time 32 func NewTime(time time.Time) Time { 33 return Time{time} 34 } 35 36 // Date returns the Time corresponding to the supplied parameters 37 // by wrapping time.Date. 38 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { 39 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} 40 } 41 42 // Now returns the current local time. 43 func Now() Time { 44 return Time{time.Now()} 45 } 46 47 // IsZero returns true if the value is nil or time is zero. 48 func (t *Time) IsZero() bool { 49 if t == nil { 50 return true 51 } 52 return t.Time.IsZero() 53 } 54 55 // Before reports whether the time instant t is before u. 56 func (t Time) Before(u Time) bool { 57 return t.Time.Before(u.Time) 58 } 59 60 // Equal reports whether the time instant t is equal to u. 61 func (t Time) Equal(u Time) bool { 62 return t.Time.Equal(u.Time) 63 } 64 65 // Unix returns the local time corresponding to the given Unix time 66 // by wrapping time.Unix. 67 func Unix(sec int64, nsec int64) Time { 68 return Time{time.Unix(sec, nsec)} 69 } 70 71 // Rfc3339Copy returns a copy of the Time at second-level precision. 72 func (t Time) Rfc3339Copy() Time { 73 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339)) 74 return Time{copied} 75 } 76 77 // UnmarshalJSON implements the json.Unmarshaller interface. 78 func (t *Time) UnmarshalJSON(b []byte) error { 79 if len(b) == 4 && string(b) == "null" { 80 t.Time = time.Time{} 81 return nil 82 } 83 84 var str string 85 json.Unmarshal(b, &str) 86 87 pt, err := time.Parse(time.RFC3339, str) 88 if err != nil { 89 return err 90 } 91 92 t.Time = pt.Local() 93 return nil 94 } 95 96 // MarshalJSON implements the json.Marshaler interface. 97 func (t Time) MarshalJSON() ([]byte, error) { 98 if t.IsZero() { 99 // Encode unset/nil objects as JSON's "null". 100 return []byte("null"), nil 101 } 102 103 return json.Marshal(t.UTC().Format(time.RFC3339)) 104 }