blob: 14841be512ae5292a2f2fd6dde15aace4b9ea1a4 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2016 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 declared in time_proto.go
24
25// Timestamp returns the Time as a new Timestamp value.
26func (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.
37func (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.
45func (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.
59func (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.
67func (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}