github.com/timstclair/heapster@v0.20.0-alpha1/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 type Time struct { 30 time.Time 31 } 32 33 // NewTime returns a wrapped instance of the provided time 34 func NewTime(time time.Time) Time { 35 return Time{time} 36 } 37 38 // Date returns the Time corresponding to the supplied parameters 39 // by wrapping time.Date. 40 func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time { 41 return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)} 42 } 43 44 // Now returns the current local time. 45 func Now() Time { 46 return Time{time.Now()} 47 } 48 49 // IsZero returns true if the value is nil or time is zero. 50 func (t *Time) IsZero() bool { 51 if t == nil { 52 return true 53 } 54 return t.Time.IsZero() 55 } 56 57 // Before reports whether the time instant t is before u. 58 func (t Time) Before(u Time) bool { 59 return t.Time.Before(u.Time) 60 } 61 62 // Equal reports whether the time instant t is equal to u. 63 func (t Time) Equal(u Time) bool { 64 return t.Time.Equal(u.Time) 65 } 66 67 // Unix returns the local time corresponding to the given Unix time 68 // by wrapping time.Unix. 69 func Unix(sec int64, nsec int64) Time { 70 return Time{time.Unix(sec, nsec)} 71 } 72 73 // Rfc3339Copy returns a copy of the Time at second-level precision. 74 func (t Time) Rfc3339Copy() Time { 75 copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339)) 76 return Time{copied} 77 } 78 79 // UnmarshalJSON implements the json.Unmarshaller interface. 80 func (t *Time) UnmarshalJSON(b []byte) error { 81 if len(b) == 4 && string(b) == "null" { 82 t.Time = time.Time{} 83 return nil 84 } 85 86 var str string 87 json.Unmarshal(b, &str) 88 89 pt, err := time.Parse(time.RFC3339, str) 90 if err != nil { 91 return err 92 } 93 94 t.Time = pt.Local() 95 return nil 96 } 97 98 // MarshalJSON implements the json.Marshaler interface. 99 func (t Time) MarshalJSON() ([]byte, error) { 100 if t.IsZero() { 101 // Encode unset/nil objects as JSON's "null". 102 return []byte("null"), nil 103 } 104 105 return json.Marshal(t.UTC().Format(time.RFC3339)) 106 } 107 108 // Fuzz satisfies fuzz.Interface. 109 func (t *Time) Fuzz(c fuzz.Continue) { 110 if t == nil { 111 return 112 } 113 // Allow for about 1000 years of randomness. Leave off nanoseconds 114 // because JSON doesn't represent them so they can't round-trip 115 // properly. 116 t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 0) 117 } 118 119 var _ fuzz.Interface = &Time{}