github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/time_proto.go (about) 1 /* 2 Copyright 2015 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 "time" 21 ) 22 23 // Timestamp is a struct that is equivalent to Time, but intended for 24 // protobuf marshalling/unmarshalling. It is generated into a serialization 25 // that matches Time. Do not use in Go structs. 26 type Timestamp struct { 27 // Represents seconds of UTC time since Unix epoch 28 // 1970-01-01T00:00:00Z. Must be from 0001-01-01T00:00:00Z to 29 // 9999-12-31T23:59:59Z inclusive. 30 Seconds int64 `json:"seconds" protobuf:"varint,1,opt,name=seconds"` 31 // Non-negative fractions of a second at nanosecond resolution. Negative 32 // second values with fractions must still have non-negative nanos values 33 // that count forward in time. Must be from 0 to 999,999,999 34 // inclusive. This field may be limited in precision depending on context. 35 Nanos int32 `json:"nanos" protobuf:"varint,2,opt,name=nanos"` 36 } 37 38 // Timestamp returns the Time as a new Timestamp value. 39 func (m *Time) ProtoTime() *Timestamp { 40 if m == nil { 41 return &Timestamp{} 42 } 43 return &Timestamp{ 44 Seconds: m.Time.Unix(), 45 // leaving this here for the record. our JSON only handled seconds, so this results in writes by 46 // protobuf clients storing values that aren't read by json clients, which results in unexpected 47 // field mutation, which fails various validation and equality code. 48 // Nanos: int32(m.Time.Nanosecond()), 49 } 50 } 51 52 // Size implements the protobuf marshalling interface. 53 func (m *Time) Size() (n int) { 54 if m == nil || m.Time.IsZero() { 55 return 0 56 } 57 return m.ProtoTime().Size() 58 } 59 60 // Reset implements the protobuf marshalling interface. 61 func (m *Time) Unmarshal(data []byte) error { 62 if len(data) == 0 { 63 m.Time = time.Time{} 64 return nil 65 } 66 p := Timestamp{} 67 if err := p.Unmarshal(data); err != nil { 68 return err 69 } 70 // leaving this here for the record. our JSON only handled seconds, so this results in writes by 71 // protobuf clients storing values that aren't read by json clients, which results in unexpected 72 // field mutation, which fails various validation and equality code. 73 // m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local() 74 m.Time = time.Unix(p.Seconds, int64(0)).Local() 75 return nil 76 } 77 78 // Marshal implements the protobuf marshaling interface. 79 func (m *Time) Marshal() (data []byte, err error) { 80 if m == nil || m.Time.IsZero() { 81 return nil, nil 82 } 83 return m.ProtoTime().Marshal() 84 } 85 86 // MarshalTo implements the protobuf marshaling interface. 87 func (m *Time) MarshalTo(data []byte) (int, error) { 88 if m == nil || m.Time.IsZero() { 89 return 0, nil 90 } 91 return m.ProtoTime().MarshalTo(data) 92 } 93 94 // MarshalToSizedBuffer implements the protobuf reverse marshaling interface. 95 func (m *Time) MarshalToSizedBuffer(data []byte) (int, error) { 96 if m == nil || m.Time.IsZero() { 97 return 0, nil 98 } 99 return m.ProtoTime().MarshalToSizedBuffer(data) 100 }