-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathtime.go
More file actions
275 lines (215 loc) · 6.12 KB
/
time.go
File metadata and controls
275 lines (215 loc) · 6.12 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package pgtype
import (
"database/sql/driver"
"encoding/binary"
"fmt"
"strconv"
"github.com/jackc/pgx/v5/internal/pgio"
)
type TimeScanner interface {
ScanTime(v Time) error
}
type TimeValuer interface {
TimeValue() (Time, error)
}
// Time represents the PostgreSQL time type. The PostgreSQL time is a time of day without time zone.
//
// Time is represented as the number of microseconds since midnight in the same way that PostgreSQL does. Other time and
// date types in pgtype can use time.Time as the underlying representation. However, pgtype.Time type cannot due to
// needing to handle 24:00:00. time.Time converts that to 00:00:00 on the following day.
//
// The time with time zone type is not supported. Use of time with time zone is discouraged by the PostgreSQL documentation.
type Time struct {
Microseconds int64 // Number of microseconds since midnight
Valid bool
}
// ScanTime implements the [TimeScanner] interface.
func (t *Time) ScanTime(v Time) error {
*t = v
return nil
}
// TimeValue implements the [TimeValuer] interface.
func (t Time) TimeValue() (Time, error) {
return t, nil
}
// Scan implements the [database/sql.Scanner] interface.
func (t *Time) Scan(src any) error {
if src == nil {
*t = Time{}
return nil
}
switch src := src.(type) {
case string:
err := scanPlanTextAnyToTimeScanner{}.Scan([]byte(src), t)
if err != nil {
t.Microseconds = 0
t.Valid = false
}
return err
}
return fmt.Errorf("cannot scan %T", src)
}
// Value implements the [database/sql/driver.Valuer] interface.
func (t Time) Value() (driver.Value, error) {
if !t.Valid {
return nil, nil
}
buf, err := TimeCodec{}.PlanEncode(nil, 0, TextFormatCode, t).Encode(t, nil)
if err != nil {
return nil, err
}
return string(buf), err
}
type TimeCodec struct{}
func (TimeCodec) FormatSupported(format int16) bool {
return format == TextFormatCode || format == BinaryFormatCode
}
func (TimeCodec) PreferredFormat() int16 {
return BinaryFormatCode
}
func (TimeCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan {
if _, ok := value.(TimeValuer); !ok {
return nil
}
switch format {
case BinaryFormatCode:
return encodePlanTimeCodecBinary{}
case TextFormatCode:
return encodePlanTimeCodecText{}
}
return nil
}
type encodePlanTimeCodecBinary struct{}
func (encodePlanTimeCodecBinary) Encode(value any, buf []byte) (newBuf []byte, err error) {
t, err := value.(TimeValuer).TimeValue()
if err != nil {
return nil, err
}
if !t.Valid {
return nil, nil
}
return pgio.AppendInt64(buf, t.Microseconds), nil
}
type encodePlanTimeCodecText struct{}
func (encodePlanTimeCodecText) Encode(value any, buf []byte) (newBuf []byte, err error) {
t, err := value.(TimeValuer).TimeValue()
if err != nil {
return nil, err
}
if !t.Valid {
return nil, nil
}
usec := t.Microseconds
hours := usec / microsecondsPerHour
usec -= hours * microsecondsPerHour
minutes := usec / microsecondsPerMinute
usec -= minutes * microsecondsPerMinute
seconds := usec / microsecondsPerSecond
usec -= seconds * microsecondsPerSecond
s := fmt.Sprintf("%02d:%02d:%02d.%06d", hours, minutes, seconds, usec)
return append(buf, s...), nil
}
func (TimeCodec) PlanScan(m *Map, oid uint32, format int16, target any) ScanPlan {
switch format {
case BinaryFormatCode:
switch target.(type) {
case TimeScanner:
return scanPlanBinaryTimeToTimeScanner{}
case TextScanner:
return scanPlanBinaryTimeToTextScanner{}
}
case TextFormatCode:
switch target.(type) {
case TimeScanner:
return scanPlanTextAnyToTimeScanner{}
}
}
return nil
}
type scanPlanBinaryTimeToTimeScanner struct{}
func (scanPlanBinaryTimeToTimeScanner) Scan(src []byte, dst any) error {
scanner := (dst).(TimeScanner)
if src == nil {
return scanner.ScanTime(Time{})
}
if len(src) != 8 {
return fmt.Errorf("invalid length for time: %v", len(src))
}
usec := int64(binary.BigEndian.Uint64(src))
return scanner.ScanTime(Time{Microseconds: usec, Valid: true})
}
type scanPlanBinaryTimeToTextScanner struct{}
func (scanPlanBinaryTimeToTextScanner) Scan(src []byte, dst any) error {
ts, ok := (dst).(TextScanner)
if !ok {
return ErrScanTargetTypeChanged
}
if src == nil {
return ts.ScanText(Text{})
}
if len(src) != 8 {
return fmt.Errorf("invalid length for time: %v", len(src))
}
usec := int64(binary.BigEndian.Uint64(src))
tim := Time{Microseconds: usec, Valid: true}
buf, err := TimeCodec{}.PlanEncode(nil, 0, TextFormatCode, tim).Encode(tim, nil)
if err != nil {
return err
}
return ts.ScanText(Text{String: string(buf), Valid: true})
}
type scanPlanTextAnyToTimeScanner struct{}
func (scanPlanTextAnyToTimeScanner) Scan(src []byte, dst any) error {
scanner := (dst).(TimeScanner)
if src == nil {
return scanner.ScanTime(Time{})
}
s := string(src)
if len(s) < 8 || s[2] != ':' || s[5] != ':' {
return fmt.Errorf("cannot decode %v into Time", s)
}
hours, err := strconv.ParseInt(s[0:2], 10, 64)
if err != nil {
return fmt.Errorf("cannot decode %v into Time", s)
}
usec := hours * microsecondsPerHour
minutes, err := strconv.ParseInt(s[3:5], 10, 64)
if err != nil {
return fmt.Errorf("cannot decode %v into Time", s)
}
usec += minutes * microsecondsPerMinute
seconds, err := strconv.ParseInt(s[6:8], 10, 64)
if err != nil {
return fmt.Errorf("cannot decode %v into Time", s)
}
usec += seconds * microsecondsPerSecond
if len(s) > 9 {
if s[8] != '.' || len(s) > 15 {
return fmt.Errorf("cannot decode %v into Time", s)
}
fraction := s[9:]
n, err := strconv.ParseInt(fraction, 10, 64)
if err != nil {
return fmt.Errorf("cannot decode %v into Time", s)
}
for i := len(fraction); i < 6; i++ {
n *= 10
}
usec += n
}
return scanner.ScanTime(Time{Microseconds: usec, Valid: true})
}
func (c TimeCodec) DecodeDatabaseSQLValue(m *Map, oid uint32, format int16, src []byte) (driver.Value, error) {
return codecDecodeToTextFormat(c, m, oid, format, src)
}
func (c TimeCodec) DecodeValue(m *Map, oid uint32, format int16, src []byte) (any, error) {
if src == nil {
return nil, nil
}
var t Time
err := codecScan(c, m, oid, format, src, &t)
if err != nil {
return nil, err
}
return t, nil
}