Zack Williams | e940c7a | 2019-08-21 14:25:39 -0700 | [diff] [blame] | 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 | "time" |
| 21 | ) |
| 22 | |
| 23 | // Timestamp is declared in time_proto.go |
| 24 | |
| 25 | // Timestamp returns the Time as a new Timestamp value. |
| 26 | func (m *MicroTime) ProtoMicroTime() *Timestamp { |
| 27 | if m == nil { |
| 28 | return &Timestamp{} |
| 29 | } |
| 30 | return &Timestamp{ |
| 31 | Seconds: m.Time.Unix(), |
| 32 | Nanos: int32(m.Time.Nanosecond()), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | // Size implements the protobuf marshalling interface. |
| 37 | func (m *MicroTime) Size() (n int) { |
| 38 | if m == nil || m.Time.IsZero() { |
| 39 | return 0 |
| 40 | } |
| 41 | return m.ProtoMicroTime().Size() |
| 42 | } |
| 43 | |
| 44 | // Reset implements the protobuf marshalling interface. |
| 45 | func (m *MicroTime) Unmarshal(data []byte) error { |
| 46 | if len(data) == 0 { |
| 47 | m.Time = time.Time{} |
| 48 | return nil |
| 49 | } |
| 50 | p := Timestamp{} |
| 51 | if err := p.Unmarshal(data); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local() |
| 55 | return nil |
| 56 | } |
| 57 | |
| 58 | // Marshal implements the protobuf marshalling interface. |
| 59 | func (m *MicroTime) Marshal() (data []byte, err error) { |
| 60 | if m == nil || m.Time.IsZero() { |
| 61 | return nil, nil |
| 62 | } |
| 63 | return m.ProtoMicroTime().Marshal() |
| 64 | } |
| 65 | |
| 66 | // MarshalTo implements the protobuf marshalling interface. |
| 67 | func (m *MicroTime) MarshalTo(data []byte) (int, error) { |
| 68 | if m == nil || m.Time.IsZero() { |
| 69 | return 0, nil |
| 70 | } |
| 71 | return m.ProtoMicroTime().MarshalTo(data) |
| 72 | } |