-
Notifications
You must be signed in to change notification settings - Fork 1
/
timestamp.go
45 lines (38 loc) · 945 Bytes
/
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
package protosql
import (
"database/sql/driver"
"errors"
"time"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/known/timestamppb"
)
type Timestamp struct {
timestamppb.Timestamp
}
func NewTimestamp(stdtime time.Time) *Timestamp {
return &Timestamp{
Timestamp: *timestamppb.New(stdtime),
}
}
// Scan implements sql.Scanner for Timestamp.
func (t *Timestamp) Scan(value interface{}) error {
stdtime, ok := value.(time.Time)
if !ok {
return errors.New("cannot scan unknown type into Timestamp")
}
t.Timestamp = *timestamppb.New(stdtime)
return nil
}
// Value implements driver.Valuer for Timestamp.
func (t *Timestamp) Value() (driver.Value, error) {
if t == nil {
return nil, nil
}
return t.Timestamp.AsTime(), nil
}
func (t *Timestamp) ProtoReflect() protoreflect.Message {
if t == nil {
return (×tamppb.Timestamp{}).ProtoReflect()
}
return t.Timestamp.ProtoReflect()
}