timestamp.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package ptypes
  5. // This file implements operations on google.protobuf.Timestamp.
  6. import (
  7. "errors"
  8. "fmt"
  9. "time"
  10. tspb "github.com/golang/protobuf/ptypes/timestamp"
  11. )
  12. const (
  13. // Seconds field of the earliest valid Timestamp.
  14. // This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
  15. minValidSeconds = -62135596800
  16. // Seconds field just after the latest valid Timestamp.
  17. // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix().
  18. maxValidSeconds = 253402300800
  19. )
  20. // validateTimestamp determines whether a Timestamp is valid.
  21. // A valid timestamp represents a time in the range
  22. // [0001-01-01, 10000-01-01) and has a Nanos field
  23. // in the range [0, 1e9).
  24. //
  25. // If the Timestamp is valid, validateTimestamp returns nil.
  26. // Otherwise, it returns an error that describes
  27. // the problem.
  28. //
  29. // Every valid Timestamp can be represented by a time.Time, but the converse is not true.
  30. func validateTimestamp(ts *tspb.Timestamp) error {
  31. if ts == nil {
  32. return errors.New("timestamp: nil Timestamp")
  33. }
  34. if ts.Seconds < minValidSeconds {
  35. return fmt.Errorf("timestamp: %v before 0001-01-01", ts)
  36. }
  37. if ts.Seconds >= maxValidSeconds {
  38. return fmt.Errorf("timestamp: %v after 10000-01-01", ts)
  39. }
  40. if ts.Nanos < 0 || ts.Nanos >= 1e9 {
  41. return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts)
  42. }
  43. return nil
  44. }
  45. // Timestamp converts a google.protobuf.Timestamp proto to a time.Time.
  46. // It returns an error if the argument is invalid.
  47. //
  48. // Unlike most Go functions, if Timestamp returns an error, the first return value
  49. // is not the zero time.Time. Instead, it is the value obtained from the
  50. // time.Unix function when passed the contents of the Timestamp, in the UTC
  51. // locale. This may or may not be a meaningful time; many invalid Timestamps
  52. // do map to valid time.Times.
  53. //
  54. // A nil Timestamp returns an error. The first return value in that case is
  55. // undefined.
  56. func Timestamp(ts *tspb.Timestamp) (time.Time, error) {
  57. // Don't return the zero value on error, because corresponds to a valid
  58. // timestamp. Instead return whatever time.Unix gives us.
  59. var t time.Time
  60. if ts == nil {
  61. t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp
  62. } else {
  63. t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
  64. }
  65. return t, validateTimestamp(ts)
  66. }
  67. // TimestampNow returns a google.protobuf.Timestamp for the current time.
  68. func TimestampNow() *tspb.Timestamp {
  69. ts, err := TimestampProto(time.Now())
  70. if err != nil {
  71. panic("ptypes: time.Now() out of Timestamp range")
  72. }
  73. return ts
  74. }
  75. // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto.
  76. // It returns an error if the resulting Timestamp is invalid.
  77. func TimestampProto(t time.Time) (*tspb.Timestamp, error) {
  78. ts := &tspb.Timestamp{
  79. Seconds: t.Unix(),
  80. Nanos: int32(t.Nanosecond()),
  81. }
  82. if err := validateTimestamp(ts); err != nil {
  83. return nil, err
  84. }
  85. return ts, nil
  86. }
  87. // TimestampString returns the RFC 3339 string for valid Timestamps. For invalid
  88. // Timestamps, it returns an error message in parentheses.
  89. func TimestampString(ts *tspb.Timestamp) string {
  90. t, err := Timestamp(ts)
  91. if err != nil {
  92. return fmt.Sprintf("(%v)", err)
  93. }
  94. return t.Format(time.RFC3339Nano)
  95. }