timestamp.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2016 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. package proto
  32. // This file implements operations on google.protobuf.Timestamp.
  33. import (
  34. "errors"
  35. "fmt"
  36. "time"
  37. )
  38. const (
  39. // Seconds field of the earliest valid Timestamp.
  40. // This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
  41. minValidSeconds = -62135596800
  42. // Seconds field just after the latest valid Timestamp.
  43. // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
  44. maxValidSeconds = 253402300800
  45. )
  46. // validateTimestamp determines whether a Timestamp is valid.
  47. // A valid timestamp represents a time in the range
  48. // [0001-01-01, 10000-01-01) and has a Nanos field
  49. // in the range [0, 1e9).
  50. //
  51. // If the Timestamp is valid, validateTimestamp returns nil.
  52. // Otherwise, it returns an error that describes
  53. // the problem.
  54. //
  55. // Every valid Timestamp can be represented by a time.Time, but the converse is not true.
  56. func validateTimestamp(ts *timestamp) error {
  57. if ts == nil {
  58. return errors.New("timestamp: nil Timestamp")
  59. }
  60. if ts.Seconds < minValidSeconds {
  61. return fmt.Errorf("timestamp: %#v before 0001-01-01", ts)
  62. }
  63. if ts.Seconds >= maxValidSeconds {
  64. return fmt.Errorf("timestamp: %#v after 10000-01-01", ts)
  65. }
  66. if ts.Nanos < 0 || ts.Nanos >= 1e9 {
  67. return fmt.Errorf("timestamp: %#v: nanos not in range [0, 1e9)", ts)
  68. }
  69. return nil
  70. }
  71. // TimestampFromProto converts a google.protobuf.Timestamp proto to a time.Time.
  72. // It returns an error if the argument is invalid.
  73. //
  74. // Unlike most Go functions, if Timestamp returns an error, the first return value
  75. // is not the zero time.Time. Instead, it is the value obtained from the
  76. // time.Unix function when passed the contents of the Timestamp, in the UTC
  77. // locale. This may or may not be a meaningful time; many invalid Timestamps
  78. // do map to valid time.Times.
  79. //
  80. // A nil Timestamp returns an error. The first return value in that case is
  81. // undefined.
  82. func timestampFromProto(ts *timestamp) (time.Time, error) {
  83. // Don't return the zero value on error, because corresponds to a valid
  84. // timestamp. Instead return whatever time.Unix gives us.
  85. var t time.Time
  86. if ts == nil {
  87. t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp
  88. } else {
  89. t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
  90. }
  91. return t, validateTimestamp(ts)
  92. }
  93. // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
  94. // It returns an error if the resulting Timestamp is invalid.
  95. func timestampProto(t time.Time) (*timestamp, error) {
  96. seconds := t.Unix()
  97. nanos := int32(t.Sub(time.Unix(seconds, 0)))
  98. ts := &timestamp{
  99. Seconds: seconds,
  100. Nanos: nanos,
  101. }
  102. if err := validateTimestamp(ts); err != nil {
  103. return nil, err
  104. }
  105. return ts, nil
  106. }