time_proto.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 v1
  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 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. // leaving this here for the record. our JSON only handled seconds, so this results in writes by
  39. // protobuf clients storing values that aren't read by json clients, which results in unexpected
  40. // field mutation, which fails various validation and equality code.
  41. // Nanos: int32(m.Time.Nanosecond()),
  42. }
  43. }
  44. // Size implements the protobuf marshalling interface.
  45. func (m *Time) Size() (n int) {
  46. if m == nil || m.Time.IsZero() {
  47. return 0
  48. }
  49. return m.ProtoTime().Size()
  50. }
  51. // Reset implements the protobuf marshalling interface.
  52. func (m *Time) Unmarshal(data []byte) error {
  53. if len(data) == 0 {
  54. m.Time = time.Time{}
  55. return nil
  56. }
  57. p := Timestamp{}
  58. if err := p.Unmarshal(data); err != nil {
  59. return err
  60. }
  61. // leaving this here for the record. our JSON only handled seconds, so this results in writes by
  62. // protobuf clients storing values that aren't read by json clients, which results in unexpected
  63. // field mutation, which fails various validation and equality code.
  64. // m.Time = time.Unix(p.Seconds, int64(p.Nanos)).Local()
  65. m.Time = time.Unix(p.Seconds, int64(0)).Local()
  66. return nil
  67. }
  68. // Marshal implements the protobuf marshalling interface.
  69. func (m *Time) Marshal() (data []byte, err error) {
  70. if m == nil || m.Time.IsZero() {
  71. return nil, nil
  72. }
  73. return m.ProtoTime().Marshal()
  74. }
  75. // MarshalTo implements the protobuf marshalling interface.
  76. func (m *Time) MarshalTo(data []byte) (int, error) {
  77. if m == nil || m.Time.IsZero() {
  78. return 0, nil
  79. }
  80. return m.ProtoTime().MarshalTo(data)
  81. }