github.com/aclisp/heapster@v0.19.2-0.20160613100040-51756f899a96/Godeps/_workspace/src/k8s.io/kubernetes/pkg/api/unversioned/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 unversioned 18 19 import ( 20 "encoding/json" 21 "time" 22 23 "github.com/google/gofuzz" 24 ) 25 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. 29 // 30 // +protobuf.options.marshal=false 31 // +protobuf.as=Timestamp 32 type Time struct { 33 time.Time `protobuf:"-"` 34 } 35 36 // NewTime returns a wrapped instance of the provided time 37 func NewTime(time time.Time) Time { 38 return Time{time} 39 } 40 41 // Date returns the Time corresponding to the supplied parameters 42 // by wrapping time.Date. 43 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { 44 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} 45 } 46 47 // Now returns the current local time. 48 func Now() Time { 49 return Time{time.Now()} 50 } 51 52 // IsZero returns true if the value is nil or time is zero. 53 func (t *Time) IsZero() bool { 54 if t == nil { 55 return true 56 } 57 return t.Time.IsZero() 58 } 59 60 // Before reports whether the time instant t is before u. 61 func (t Time) Before(u Time) bool { 62 return t.Time.Before(u.Time) 63 } 64 65 // Equal reports whether the time instant t is equal to u. 66 func (t Time) Equal(u Time) bool { 67 return t.Time.Equal(u.Time) 68 } 69 70 // Unix returns the local time corresponding to the given Unix time 71 // by wrapping time.Unix. 72 func Unix(sec int64, nsec int64) Time { 73 return Time{time.Unix(sec, nsec)} 74 } 75 76 // Rfc3339Copy returns a copy of the Time at second-level precision. 77 func (t Time) Rfc3339Copy() Time { 78 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339)) 79 return Time{copied} 80 } 81 82 // UnmarshalJSON implements the json.Unmarshaller interface. 83 func (t *Time) UnmarshalJSON(b []byte) error { 84 if len(b) == 4 && string(b) == "null" { 85 t.Time = time.Time{} 86 return nil 87 } 88 89 var str string 90 json.Unmarshal(b, &str) 91 92 pt, err := time.Parse(time.RFC3339, str) 93 if err != nil { 94 return err 95 } 96 97 t.Time = pt.Local() 98 return nil 99 } 100 101 // UnmarshalQueryParameter converts from a URL query parameter value to an object 102 func (t *Time) UnmarshalQueryParameter(str string) error { 103 if len(str) == 0 { 104 t.Time = time.Time{} 105 return nil 106 } 107 // Tolerate requests from older clients that used JSON serialization to build query params 108 if len(str) == 4 && str == "null" { 109 t.Time = time.Time{} 110 return nil 111 } 112 113 pt, err := time.Parse(time.RFC3339, str) 114 if err != nil { 115 return err 116 } 117 118 t.Time = pt.Local() 119 return nil 120 } 121 122 // MarshalJSON implements the json.Marshaler interface. 123 func (t Time) MarshalJSON() ([]byte, error) { 124 if t.IsZero() { 125 // Encode unset/nil objects as JSON's "null". 126 return []byte("null"), nil 127 } 128 129 return json.Marshal(t.UTC().Format(time.RFC3339)) 130 } 131 132 // MarshalQueryParameter converts to a URL query parameter value 133 func (t Time) MarshalQueryParameter() (string, error) { 134 if t.IsZero() { 135 // Encode unset/nil objects as an empty string 136 return "", nil 137 } 138 139 return t.UTC().Format(time.RFC3339), nil 140 } 141 142 // Fuzz satisfies fuzz.Interface. 143 func (t *Time) Fuzz(c fuzz.Continue) { 144 if t == nil { 145 return 146 } 147 // Allow for about 1000 years of randomness. Leave off nanoseconds 148 // because JSON doesn't represent them so they can't round-trip 149 // properly. 150 t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0) 151 } 152 153 var _ fuzz.Interface = &Time{}