khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 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. |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 4 | |
| 5 | package ptypes |
| 6 | |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 7 | import ( |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "time" |
| 11 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 12 | timestamppb "github.com/golang/protobuf/ptypes/timestamp" |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 13 | ) |
| 14 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 15 | // Range of google.protobuf.Duration as specified in timestamp.proto. |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 16 | const ( |
| 17 | // Seconds field of the earliest valid Timestamp. |
| 18 | // This is time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Unix(). |
| 19 | minValidSeconds = -62135596800 |
| 20 | // Seconds field just after the latest valid Timestamp. |
| 21 | // This is time.Date(10000, 1, 1, 0, 0, 0, 0, time.UTC).Unix(). |
| 22 | maxValidSeconds = 253402300800 |
| 23 | ) |
| 24 | |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 25 | // Timestamp converts a timestamppb.Timestamp to a time.Time. |
| 26 | // It returns an error if the argument is invalid. |
| 27 | // |
| 28 | // Unlike most Go functions, if Timestamp returns an error, the first return |
| 29 | // value is not the zero time.Time. Instead, it is the value obtained from the |
| 30 | // time.Unix function when passed the contents of the Timestamp, in the UTC |
| 31 | // locale. This may or may not be a meaningful time; many invalid Timestamps |
| 32 | // do map to valid time.Times. |
| 33 | // |
| 34 | // A nil Timestamp returns an error. The first return value in that case is |
| 35 | // undefined. |
| 36 | // |
| 37 | // Deprecated: Call the ts.AsTime and ts.CheckValid methods instead. |
| 38 | func Timestamp(ts *timestamppb.Timestamp) (time.Time, error) { |
| 39 | // Don't return the zero value on error, because corresponds to a valid |
| 40 | // timestamp. Instead return whatever time.Unix gives us. |
| 41 | var t time.Time |
| 42 | if ts == nil { |
| 43 | t = time.Unix(0, 0).UTC() // treat nil like the empty Timestamp |
| 44 | } else { |
| 45 | t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC() |
| 46 | } |
| 47 | return t, validateTimestamp(ts) |
| 48 | } |
| 49 | |
| 50 | // TimestampNow returns a google.protobuf.Timestamp for the current time. |
| 51 | // |
| 52 | // Deprecated: Call the timestamppb.Now function instead. |
| 53 | func TimestampNow() *timestamppb.Timestamp { |
| 54 | ts, err := TimestampProto(time.Now()) |
| 55 | if err != nil { |
| 56 | panic("ptypes: time.Now() out of Timestamp range") |
| 57 | } |
| 58 | return ts |
| 59 | } |
| 60 | |
| 61 | // TimestampProto converts the time.Time to a google.protobuf.Timestamp proto. |
| 62 | // It returns an error if the resulting Timestamp is invalid. |
| 63 | // |
| 64 | // Deprecated: Call the timestamppb.New function instead. |
| 65 | func TimestampProto(t time.Time) (*timestamppb.Timestamp, error) { |
| 66 | ts := ×tamppb.Timestamp{ |
| 67 | Seconds: t.Unix(), |
| 68 | Nanos: int32(t.Nanosecond()), |
| 69 | } |
| 70 | if err := validateTimestamp(ts); err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | return ts, nil |
| 74 | } |
| 75 | |
| 76 | // TimestampString returns the RFC 3339 string for valid Timestamps. |
| 77 | // For invalid Timestamps, it returns an error message in parentheses. |
| 78 | // |
| 79 | // Deprecated: Call the ts.AsTime method instead, |
| 80 | // followed by a call to the Format method on the time.Time value. |
| 81 | func TimestampString(ts *timestamppb.Timestamp) string { |
| 82 | t, err := Timestamp(ts) |
| 83 | if err != nil { |
| 84 | return fmt.Sprintf("(%v)", err) |
| 85 | } |
| 86 | return t.Format(time.RFC3339Nano) |
| 87 | } |
| 88 | |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 89 | // validateTimestamp determines whether a Timestamp is valid. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 90 | // A valid timestamp represents a time in the range [0001-01-01, 10000-01-01) |
| 91 | // and has a Nanos field in the range [0, 1e9). |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 92 | // |
| 93 | // If the Timestamp is valid, validateTimestamp returns nil. |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 94 | // Otherwise, it returns an error that describes the problem. |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 95 | // |
khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 96 | // Every valid Timestamp can be represented by a time.Time, |
| 97 | // but the converse is not true. |
| 98 | func validateTimestamp(ts *timestamppb.Timestamp) error { |
khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 99 | if ts == nil { |
| 100 | return errors.New("timestamp: nil Timestamp") |
| 101 | } |
| 102 | if ts.Seconds < minValidSeconds { |
| 103 | return fmt.Errorf("timestamp: %v before 0001-01-01", ts) |
| 104 | } |
| 105 | if ts.Seconds >= maxValidSeconds { |
| 106 | return fmt.Errorf("timestamp: %v after 10000-01-01", ts) |
| 107 | } |
| 108 | if ts.Nanos < 0 || ts.Nanos >= 1e9 { |
| 109 | return fmt.Errorf("timestamp: %v: nanos not in range [0, 1e9)", ts) |
| 110 | } |
| 111 | return nil |
| 112 | } |