-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
108 lines (92 loc) · 1.93 KB
/
time.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package convpb
import (
"database/sql"
"time"
"google.golang.org/protobuf/types/known/timestamppb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type TimeWrapper interface {
Commoner[TimeWrapper]
ToTimestamp() *timestamppb.Timestamp
ToStringValue(format string) *wrapperspb.StringValue
}
type timeWrapper struct {
value *time.Time
}
func (t *timeWrapper) IsNil() bool {
return t == nil || t.value == nil
}
func (t *timeWrapper) Clone() TimeWrapper {
if t.IsNil() {
return &timeWrapper{}
}
cop := *t.value
return &timeWrapper{value: &cop}
}
func (t *timeWrapper) EmptySetNil() TimeWrapper {
if t.IsNil() {
return t
}
if t.value.IsZero() {
t.value = nil
}
return t
}
func (t *timeWrapper) ToTimestamp() *timestamppb.Timestamp {
if t.IsNil() {
return nil
}
return timestamppb.New(*t.value)
}
func (t *timeWrapper) ToStringValue(layout string) *wrapperspb.StringValue {
if t.IsNil() {
return nil
}
return wrapperspb.String(t.value.Format(layout))
}
type TimestampValuer interface {
Commoner[TimestampValuer]
ToTimeRef() *time.Time
ToTime() time.Time
ToSQLNullTime() sql.NullTime
}
type timestampValuer struct {
value *timestamppb.Timestamp
}
func (t *timestampValuer) IsNil() bool {
return t == nil || t.value == nil
}
func (t *timestampValuer) Clone() TimestampValuer {
if t.IsNil() {
return ×tampValuer{}
}
return ×tampValuer{value: timestamppb.New(t.value.AsTime())}
}
func (t *timestampValuer) EmptySetNil() TimestampValuer {
if t.IsNil() {
return t
}
if !t.value.IsValid() || t.value.AsTime().IsZero() {
t.value = nil
}
return t
}
func (t *timestampValuer) ToTimeRef() *time.Time {
if t.IsNil() {
return nil
}
v := t.value.AsTime()
return &v
}
func (t *timestampValuer) ToTime() time.Time {
if t.IsNil() {
return time.Time{}
}
return t.value.AsTime()
}
func (t *timestampValuer) ToSQLNullTime() sql.NullTime {
return sql.NullTime{
Time: t.ToTime(),
Valid: !t.IsNil(),
}
}