1/* 2Copyright 2015 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package v1 18 19import ( 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. 26type 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. 39func (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. 53func (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. 61func (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 marshalling interface. 79func (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 marshalling interface. 87func (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