/
timestamptz.go
185 lines (160 loc) · 4.25 KB
/
timestamptz.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//nolint:revive,gocritic
package schema
import (
"encoding"
"fmt"
"time"
)
// const pgTimestamptzHourFormat = "2006-01-02 15:04:05.999999999Z07"
// const pgTimestamptzMinuteFormat = "2006-01-02 15:04:05.999999999Z07:00"
// const pgTimestamptzSecondFormat = "2006-01-02 15:04:05.999999999Z07:00:00"
// this is the default format used by time.Time.String()
const defaultStringFormat = "2006-01-02 15:04:05.999999999 -0700 MST"
// this is used by arrow string format (time is in UTC)
const arrowStringFormat = "2006-01-02 15:04:05.999999999"
// const microsecFromUnixEpochToY2K = 946684800 * 1000000
const (
// negativeInfinityMicrosecondOffset = -9223372036854775808
// infinityMicrosecondOffset = 9223372036854775807
)
func (dst *Timestamptz) GetStatus() Status {
return dst.Status
}
type TimestamptzTransformer interface {
TransformTimestamptz(*Timestamptz) any
}
type Timestamptz struct {
Time time.Time
Status Status
InfinityModifier InfinityModifier
}
func (*Timestamptz) Type() ValueType {
return TypeTimestamp
}
func (dst *Timestamptz) Size() int {
return 24
}
func (dst *Timestamptz) Equal(src CQType) bool {
if src == nil {
return false
}
if value, ok := src.(*Timestamptz); ok {
if dst.Status != value.Status || dst.InfinityModifier != value.InfinityModifier {
return false
}
return dst.Time.Equal(value.Time)
}
return false
}
func (dst *Timestamptz) String() string {
if dst.Status == Present {
return dst.Time.Format(time.RFC3339)
} else {
return ""
}
}
func (dst *Timestamptz) Set(src any) error {
if src == nil {
*dst = Timestamptz{Status: Null}
return nil
}
if value, ok := src.(interface{ Get() any }); ok {
value2 := value.Get()
if value2 != value {
return dst.Set(value2)
}
}
switch value := src.(type) {
case int:
*dst = Timestamptz{Time: time.Unix(int64(value), 0).UTC(), Status: Present}
case int64:
*dst = Timestamptz{Time: time.Unix(value, 0).UTC(), Status: Present}
case uint64:
*dst = Timestamptz{Time: time.Unix(int64(value), 0).UTC(), Status: Present}
case time.Time:
*dst = Timestamptz{Time: value.UTC(), Status: Present}
case *time.Time:
if value == nil {
*dst = Timestamptz{Status: Null}
} else {
return dst.Set(*value)
}
case string:
return dst.DecodeText([]byte(value))
case *string:
if value == nil {
*dst = Timestamptz{Status: Null}
} else {
return dst.Set(*value)
}
case InfinityModifier:
*dst = Timestamptz{InfinityModifier: value, Status: Present}
default:
if originalSrc, ok := underlyingTimeType(src); ok {
return dst.Set(originalSrc)
}
if value, ok := value.(encoding.TextMarshaler); ok {
s, err := value.MarshalText()
if err == nil {
return dst.Set(string(s))
}
// fall through to String() method
}
if value, ok := value.(fmt.Stringer); ok {
s := value.String()
return dst.Set(s)
}
return &ValidationError{Type: TypeTimestamp, Msg: noConversion, Value: value}
}
return nil
}
func (dst Timestamptz) Get() any {
switch dst.Status {
case Present:
if dst.InfinityModifier != None {
return dst.InfinityModifier
}
return dst.Time
case Null:
return nil
default:
return dst.Status
}
}
func (dst *Timestamptz) DecodeText(src []byte) error {
if len(src) == 0 {
*dst = Timestamptz{Status: Null}
return nil
}
sbuf := string(src)
switch sbuf {
case "infinity":
*dst = Timestamptz{Status: Present, InfinityModifier: Infinity}
case "-infinity":
*dst = Timestamptz{Status: Present, InfinityModifier: -Infinity}
default:
var tim time.Time
var err error
if len(sbuf) > len(defaultStringFormat)+1 && sbuf[len(defaultStringFormat)+1] == 'm' {
sbuf = sbuf[:len(defaultStringFormat)]
}
// there is no good way of detecting format so we just try few of them
tim, err = time.Parse(time.RFC3339, sbuf)
if err == nil {
*dst = Timestamptz{Time: tim.UTC(), Status: Present}
return nil
}
tim, err = time.Parse(defaultStringFormat, sbuf)
if err == nil {
*dst = Timestamptz{Time: tim.UTC(), Status: Present}
return nil
}
tim, err = time.Parse(arrowStringFormat, sbuf)
if err == nil {
*dst = Timestamptz{Time: tim.UTC(), Status: Present}
return nil
}
return &ValidationError{Type: TypeTimestamp, Msg: "cannot parse timestamp", Value: sbuf, Err: err}
}
return nil
}