Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / apimachinery / pkg / apis / meta / v1 / micro_time.go
1 /*
2 Copyright 2016 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         "github.com/google/gofuzz"
24 )
25
26 const RFC3339Micro = "2006-01-02T15:04:05.000000Z07:00"
27
28 // MicroTime is version of Time with microsecond level precision.
29 //
30 // +protobuf.options.marshal=false
31 // +protobuf.as=Timestamp
32 // +protobuf.options.(gogoproto.goproto_stringer)=false
33 type MicroTime struct {
34         time.Time `protobuf:"-"`
35 }
36
37 // DeepCopy returns a deep-copy of the MicroTime 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 *MicroTime) DeepCopyInto(out *MicroTime) {
41         *out = *t
42 }
43
44 // String returns the representation of the time.
45 func (t MicroTime) String() string {
46         return t.Time.String()
47 }
48
49 // NewMicroTime returns a wrapped instance of the provided time
50 func NewMicroTime(time time.Time) MicroTime {
51         return MicroTime{time}
52 }
53
54 // DateMicro returns the MicroTime corresponding to the supplied parameters
55 // by wrapping time.Date.
56 func DateMicro(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) MicroTime {
57         return MicroTime{time.Date(year, month, day, hour, min, sec, nsec, loc)}
58 }
59
60 // NowMicro returns the current local time.
61 func NowMicro() MicroTime {
62         return MicroTime{time.Now()}
63 }
64
65 // IsZero returns true if the value is nil or time is zero.
66 func (t *MicroTime) IsZero() bool {
67         if t == nil {
68                 return true
69         }
70         return t.Time.IsZero()
71 }
72
73 // Before reports whether the time instant t is before u.
74 func (t *MicroTime) Before(u *MicroTime) bool {
75         return t.Time.Before(u.Time)
76 }
77
78 // Equal reports whether the time instant t is equal to u.
79 func (t *MicroTime) Equal(u *MicroTime) bool {
80         return t.Time.Equal(u.Time)
81 }
82
83 // BeforeTime reports whether the time instant t is before second-lever precision u.
84 func (t *MicroTime) BeforeTime(u *Time) bool {
85         return t.Time.Before(u.Time)
86 }
87
88 // EqualTime reports whether the time instant t is equal to second-lever precision u.
89 func (t *MicroTime) EqualTime(u *Time) bool {
90         return t.Time.Equal(u.Time)
91 }
92
93 // UnixMicro returns the local time corresponding to the given Unix time
94 // by wrapping time.Unix.
95 func UnixMicro(sec int64, nsec int64) MicroTime {
96         return MicroTime{time.Unix(sec, nsec)}
97 }
98
99 // UnmarshalJSON implements the json.Unmarshaller interface.
100 func (t *MicroTime) UnmarshalJSON(b []byte) error {
101         if len(b) == 4 && string(b) == "null" {
102                 t.Time = time.Time{}
103                 return nil
104         }
105
106         var str string
107         err := json.Unmarshal(b, &str)
108         if err != nil {
109                 return err
110         }
111
112         pt, err := time.Parse(RFC3339Micro, str)
113         if err != nil {
114                 return err
115         }
116
117         t.Time = pt.Local()
118         return nil
119 }
120
121 // UnmarshalQueryParameter converts from a URL query parameter value to an object
122 func (t *MicroTime) UnmarshalQueryParameter(str string) error {
123         if len(str) == 0 {
124                 t.Time = time.Time{}
125                 return nil
126         }
127         // Tolerate requests from older clients that used JSON serialization to build query params
128         if len(str) == 4 && str == "null" {
129                 t.Time = time.Time{}
130                 return nil
131         }
132
133         pt, err := time.Parse(RFC3339Micro, str)
134         if err != nil {
135                 return err
136         }
137
138         t.Time = pt.Local()
139         return nil
140 }
141
142 // MarshalJSON implements the json.Marshaler interface.
143 func (t MicroTime) MarshalJSON() ([]byte, error) {
144         if t.IsZero() {
145                 // Encode unset/nil objects as JSON's "null".
146                 return []byte("null"), nil
147         }
148
149         return json.Marshal(t.UTC().Format(RFC3339Micro))
150 }
151
152 // OpenAPISchemaType is used by the kube-openapi generator when constructing
153 // the OpenAPI spec of this type.
154 //
155 // See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators
156 func (_ MicroTime) OpenAPISchemaType() []string { return []string{"string"} }
157
158 // OpenAPISchemaFormat is used by the kube-openapi generator when constructing
159 // the OpenAPI spec of this type.
160 func (_ MicroTime) OpenAPISchemaFormat() string { return "date-time" }
161
162 // MarshalQueryParameter converts to a URL query parameter value
163 func (t MicroTime) MarshalQueryParameter() (string, error) {
164         if t.IsZero() {
165                 // Encode unset/nil objects as an empty string
166                 return "", nil
167         }
168
169         return t.UTC().Format(RFC3339Micro), nil
170 }
171
172 // Fuzz satisfies fuzz.Interface.
173 func (t *MicroTime) Fuzz(c fuzz.Continue) {
174         if t == nil {
175                 return
176         }
177         // Allow for about 1000 years of randomness. Accurate to a tenth of
178         // micro second. Leave off nanoseconds because JSON doesn't
179         // represent them so they can't round-trip properly.
180         t.Time = time.Unix(c.Rand.Int63n(1000*365*24*60*60), 1000*c.Rand.Int63n(1000000))
181 }
182
183 var _ fuzz.Interface = &MicroTime{}