123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package proto
- import (
- "errors"
- "fmt"
- "time"
- )
- const (
-
-
- minValidSeconds = -62135596800
-
-
- maxValidSeconds = 253402300800
- )
- func validateTimestamp(ts *timestamp) error {
- if ts == nil {
- return errors.New("timestamp: nil Timestamp")
- }
- if ts.Seconds < minValidSeconds {
- return fmt.Errorf("timestamp: %#v before 0001-01-01", ts)
- }
- if ts.Seconds >= maxValidSeconds {
- return fmt.Errorf("timestamp: %#v after 10000-01-01", ts)
- }
- if ts.Nanos < 0 || ts.Nanos >= 1e9 {
- return fmt.Errorf("timestamp: %#v: nanos not in range [0, 1e9)", ts)
- }
- return nil
- }
- func timestampFromProto(ts *timestamp) (time.Time, error) {
-
-
- var t time.Time
- if ts == nil {
- t = time.Unix(0, 0).UTC()
- } else {
- t = time.Unix(ts.Seconds, int64(ts.Nanos)).UTC()
- }
- return t, validateTimestamp(ts)
- }
- func timestampProto(t time.Time) (*timestamp, error) {
- seconds := t.Unix()
- nanos := int32(t.Sub(time.Unix(seconds, 0)))
- ts := ×tamp{
- Seconds: seconds,
- Nanos: nanos,
- }
- if err := validateTimestamp(ts); err != nil {
- return nil, err
- }
- return ts, nil
- }
|