-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatetime.go
166 lines (150 loc) · 2.97 KB
/
datetime.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
package mysqlx
import (
"encoding/binary"
"fmt"
"time"
)
type Date struct {
Year uint64
Month uint64
Day uint64
}
func (d Date) String() string {
return fmt.Sprintf("%04d-%02d-%02d", d.Year, d.Month, d.Day)
}
func (d *Date) Unmarshal(b []byte) error {
var i, j int
d.Year, i = binary.Uvarint(b)
if i <= 0 {
return fmt.Errorf("failed to decode year (%x)", b)
}
d.Month, j = binary.Uvarint(b[i:])
if j <= 0 {
return fmt.Errorf("failed to decode month (%x)", b)
}
i += j
d.Day, j = binary.Uvarint(b[i:])
if j <= 0 {
return fmt.Errorf("failed to decode day (%x)", b)
}
return nil
}
type NullDate struct {
Date
Valid bool
}
func (d *NullDate) Scan(src any) error {
if src == nil {
d.Valid = false
return nil
}
d.Valid = true
switch v := src.(type) {
case []byte:
return d.Date.Unmarshal(v)
case Date:
d.Date = v
return nil
}
return fmt.Errorf("unable to convert type %T to %T", src, d)
}
type DateTime struct {
Year uint64
Month uint64
Day uint64
Hour uint64
Minute uint64
Second uint64
Nanosecond uint64
}
func (dt *DateTime) Unmarshal(b []byte) error {
var i, j int
dt.Hour, dt.Minute, dt.Second, dt.Nanosecond = 0, 0, 0, 0
dt.Year, i = binary.Uvarint(b)
if i <= 0 {
return fmt.Errorf("failed to decode datetime year (%x)", b)
}
dt.Month, j = binary.Uvarint(b[i:])
if j <= 0 {
return fmt.Errorf("failed to decode datetime month (%x)", b)
}
i += j
dt.Day, j = binary.Uvarint(b[i:])
if j <= 0 {
return fmt.Errorf("failed to decode datetime day (%x)", b)
}
i += j
dt.Hour, j = binary.Uvarint(b[i:])
if j > 0 {
i += j
dt.Minute, j = binary.Uvarint(b[i:])
if j > 0 {
i += j
dt.Second, j = binary.Uvarint(b[i:])
if j > 0 {
i += j
dt.Nanosecond, j = binary.Uvarint(b[i:])
}
}
}
if j < 0 {
return fmt.Errorf("failed to decode datetime time (%x)", b)
}
return nil
}
func (dt *DateTime) Scan(src any) error {
switch v := src.(type) {
case *DateTime:
dt = v
return nil
}
return fmt.Errorf("unable to convert type %T to %T", src, dt)
}
type NullDateTime struct {
DateTime
Valid bool
}
func (dt *NullDateTime) Scan(src any) error {
if src == nil {
dt.Valid = false
return nil
}
dt.Valid = true
switch v := src.(type) {
case []byte:
return dt.DateTime.Unmarshal(v)
case DateTime:
dt.DateTime = v
return nil
}
return fmt.Errorf("unable to convert type %T to %T", src, dt)
}
func parseDuration(b []byte) (time.Duration, error) {
v, i := binary.Uvarint(b[1:])
if i < 0 {
return 0, fmt.Errorf("failed to decode time (%x)", b)
}
d := time.Duration(v) * time.Hour
if i > 0 {
i++
v, j := binary.Uvarint(b[i:])
if j > 0 {
d += time.Duration(v) * time.Minute
i += j
v, j = binary.Uvarint(b[i:])
if j > 0 {
d += time.Duration(v) * time.Second
i += j
v, j = binary.Uvarint(b[i:])
d += time.Duration(v)
}
}
if j < 0 {
return 0, fmt.Errorf("failed to decode time (%x)", b)
}
}
if b[0] == 0x01 {
return -d, nil
}
return d, nil
}