| 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 |
|
| 31 |
// truncate precision to microseconds to match JSON marshaling/unmarshaling |
| 32 |
truncatedNanoseconds := time.Duration(m.Time.Nanosecond()).Truncate(time.Microsecond) |
| 33 |
return &Timestamp{ |
| 34 |
Seconds: m.Time.Unix(), |
| 35 |
Nanos: int32(truncatedNanoseconds), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
// Size implements the protobuf marshalling interface. |
| 40 |
func (m *MicroTime) Size() (n int) { |
| 41 |
if m == nil || m.Time.IsZero() { |
| 42 |
return 0 |
| 43 |
} |
| 44 |
return m.ProtoMicroTime().Size() |
| 45 |
} |
| 46 |
|
| 47 |
// Reset implements the protobuf marshalling interface. |
| 48 |
func (m *MicroTime) Unmarshal(data []byte) error { |
| 49 |
if len(data) == 0 { |
| 50 |
m.Time = time.Time{} |
| 51 |
return nil |
| 52 |
} |
| 53 |
p := Timestamp{} |
| 54 |
if err := p.Unmarshal(data); err != nil { |
| 55 |
return err |
| 56 |
} |
| 57 |
|
| 58 |
// truncate precision to microseconds to match JSON marshaling/unmarshaling |
| 59 |
truncatedNanoseconds := time.Duration(p.Nanos).Truncate(time.Microsecond) |
| 60 |
m.Time = time.Unix(p.Seconds, int64(truncatedNanoseconds)).Local() |
| 61 |
return nil |
| 62 |
} |
| 63 |
|
| 64 |
// Marshal implements the protobuf marshalling interface. |
| 65 |
func (m *MicroTime) Marshal() (data []byte, err error) { |
| 66 |
if m == nil || m.Time.IsZero() { |
| 67 |
return nil, nil |
| 68 |
} |
| 69 |
return m.ProtoMicroTime().Marshal() |
| 70 |
} |
| 71 |
|
| 72 |
// MarshalTo implements the protobuf marshalling interface. |
| 73 |
func (m *MicroTime) MarshalTo(data []byte) (int, error) { |
| 74 |
if m == nil || m.Time.IsZero() { |
| 75 |
return 0, nil |
| 76 |
} |
| 77 |
return m.ProtoMicroTime().MarshalTo(data) |
| 78 |
} |
| 79 |
|
| 80 |
// MarshalToSizedBuffer implements the protobuf marshalling interface. |
| 81 |
func (m *MicroTime) MarshalToSizedBuffer(data []byte) (int, error) { |
| 82 |
if m == nil || m.Time.IsZero() { |
| 83 |
return 0, nil |
| 84 |
} |
| 85 |
return m.ProtoMicroTime().MarshalToSizedBuffer(data) |
| 86 |
} |