time_proto.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package unversioned
  14. import (
  15. "time"
  16. )
  17. // Timestamp is a struct that is equivalent to Time, but intended for
  18. // protobuf marshalling/unmarshalling. It is generated into a serialization
  19. // that matches Time. Do not use in Go structs.
  20. type Timestamp struct {
  21. // Represents seconds of UTC time since Unix epoch
  22. // 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to
  23. // 9999-12-31T23:59:59Z inclusive.
  24. Seconds int64 `json:"seconds" protobuf:"varint,1,opt,name=seconds"`
  25. // Non-negative fractions of a second at nanosecond resolution. Negative
  26. // second values with fractions must still have non-negative nanos values
  27. // that count forward in time. Must be from 0 to 999,999,999
  28. // inclusive. This field may be limited in precision depending on context.
  29. Nanos int32 `json:"nanos" protobuf:"varint,2,opt,name=nanos"`
  30. }
  31. // Timestamp returns the Time as a new Timestamp value.
  32. func (m *Time) ProtoTime() *Timestamp {
  33. if m == nil {
  34. return &Timestamp{}
  35. }
  36. return &Timestamp{
  37. Seconds: m.Time.Unix(),
  38. Nanos: int32(m.Time.Nanosecond()),
  39. }
  40. }
  41. // Size implements the protobuf marshalling interface.
  42. func (m *Time) Size() (n int) {
  43. if m == nil || m.Time.IsZero() {
  44. return 0
  45. }
  46. return m.ProtoTime().Size()
  47. }
  48. // Reset implements the protobuf marshalling interface.
  49. func (m *Time) Unmarshal(data []byte) error {
  50. if len(data) == 0 {
  51. m.Time = time.Time{}
  52. return nil
  53. }
  54. p := Timestamp{}
  55. if err := p.Unmarshal(data); err != nil {
  56. return err
  57. }
  58. m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local()
  59. return nil
  60. }
  61. // Marshal implements the protobuf marshalling interface.
  62. func (m *Time) Marshal() (data []byte, err error) {
  63. if m == nil || m.Time.IsZero() {
  64. return nil, nil
  65. }
  66. return m.ProtoTime().Marshal()
  67. }
  68. // MarshalTo implements the protobuf marshalling interface.
  69. func (m *Time) MarshalTo(data []byte) (int, error) {
  70. if m == nil || m.Time.IsZero() {
  71. return 0, nil
  72. }
  73. return m.ProtoTime().MarshalTo(data)
  74. }