/
timestamp.go
79 lines (69 loc) · 2.15 KB
/
timestamp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// Copyright 2022 Md Sulaiman. All rights reserved.
// https://developers.google.com/protocol-buffers/
package timestamppb
import (
"time"
"google.golang.org/protobuf/runtime/protoimpl"
)
// Now constructs a new Timestamp from the current time.
func Now() *Timestamp {
return New(time.Now())
}
// New constructs a new Timestamp from the provided time.Time.
func New(t time.Time) *Timestamp {
return &Timestamp{Seconds: int64(t.Unix()), Nanos: int32(t.Nanosecond())}
}
// AsTime converts x to a time.Time.
func (x *Timestamp) AsTime() time.Time {
return time.Unix(int64(x.GetSeconds()), int64(x.GetNanos())).UTC()
}
// IsValid reports whether the timestamp is valid.
// It is equivalent to CheckValid == nil.
func (x *Timestamp) IsValid() bool {
return x.check() == 0
}
// CheckValid returns an error if the timestamp is invalid.
// In particular, it checks whether the value represents a date that is
// in the range of 0001-01-01T00:00:00Z to 9999-12-31T23:59:59Z inclusive.
// An error is reported for a nil Timestamp.
func (x *Timestamp) CheckValid() error {
switch x.check() {
case invalidNil:
return protoimpl.X.NewError("invalid nil Timestamp")
case invalidUnderflow:
return protoimpl.X.NewError("timestamp (%v) before 0001-01-01", x)
case invalidOverflow:
return protoimpl.X.NewError("timestamp (%v) after 9999-12-31", x)
case invalidNanos:
return protoimpl.X.NewError("timestamp (%v) has out-of-range nanos", x)
default:
return nil
}
}
const (
_ = iota
invalidNil
invalidUnderflow
invalidOverflow
invalidNanos
)
func (x *Timestamp) check() uint {
const minTimestamp = -62135596800 // Seconds between 1970-01-01T00:00:00Z and 0001-01-01T00:00:00Z, inclusive
const maxTimestamp = +253402300799 // Seconds between 1970-01-01T00:00:00Z and 9999-12-31T23:59:59Z, inclusive
secs := x.GetSeconds()
nanos := x.GetNanos()
switch {
case x == nil:
return invalidNil
case secs < minTimestamp:
return invalidUnderflow
case secs > maxTimestamp:
return invalidOverflow
case nanos < 0 || nanos >= 1e9:
return invalidNanos
default:
return 0
}
}