-
Notifications
You must be signed in to change notification settings - Fork 12
/
datetime64.go
120 lines (100 loc) · 2.59 KB
/
datetime64.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
package column
import (
"encoding/binary"
"math"
"strings"
"time"
"github.com/bytehouse-cloud/driver-go/driver/lib/bytepool"
"github.com/bytehouse-cloud/driver-go/driver/lib/ch_encoding"
)
const (
dateTime64Len = 8
dateTime64FormatPrefix = "2006-01-02 15:04:05."
)
type DateTime64ColumnData struct {
precision int
timeZone *time.Location
raw []byte
isClosed bool
}
func (d *DateTime64ColumnData) ReadFromDecoder(decoder *ch_encoding.Decoder) error {
_, err := decoder.Read(d.raw)
return err
}
func (d *DateTime64ColumnData) WriteToEncoder(encoder *ch_encoding.Encoder) error {
_, err := encoder.Write(d.raw)
return err
}
func (d *DateTime64ColumnData) ReadFromValues(values []interface{}) (int, error) {
var (
t time.Time
ok bool
)
for i, value := range values {
t, ok = value.(time.Time)
if !ok {
return i, NewErrInvalidColumnType(value, t)
}
n := t.UnixNano() / int64(math.Pow10(9-d.precision))
binary.LittleEndian.PutUint64(d.raw[i*dateTime64Len:], uint64(n))
}
return len(values), nil
}
func (d *DateTime64ColumnData) ReadFromTexts(texts []string) (int, error) {
var (
t time.Time
err error
)
for i, text := range texts {
if text == "" {
binary.LittleEndian.PutUint64(d.raw[i*dateTime64Len:], 0)
continue
}
t, err = parseDateTime64Format(supportedDateTimeFormats, text, d.precision, d.timeZone)
if err != nil {
return i, err
}
n := t.UnixNano() / int64(math.Pow10(9-d.precision))
binary.LittleEndian.PutUint64(d.raw[i*dateTime64Len:], uint64(n))
}
return len(texts), nil
}
func (d *DateTime64ColumnData) get(row int) time.Time {
timeValue := int64(bufferRowToUint64(d.raw, row))
timeValue *= int64(math.Pow10(9 - d.precision))
if d.timeZone != nil {
return time.Unix(0, timeValue).In(d.timeZone)
}
return time.Unix(0, timeValue)
}
func (d *DateTime64ColumnData) GetValue(row int) interface{} {
return d.get(row)
}
func (d *DateTime64ColumnData) GetString(row int) string {
return d.get(row).Format(d.getDateTime64Format())
}
func (d *DateTime64ColumnData) Zero() interface{} {
return zeroTime
}
func (d *DateTime64ColumnData) ZeroString() string {
return zeroTime.Format(d.getDateTime64Format())
}
func (d *DateTime64ColumnData) Len() int {
return len(d.raw) / dateTime64Len
}
func (d *DateTime64ColumnData) Close() error {
if d.isClosed {
return nil
}
d.isClosed = true
bytepool.PutBytes(d.raw)
return nil
}
func (d *DateTime64ColumnData) getDateTime64Format() string {
var sb strings.Builder
sb.WriteString(dateTime64FormatPrefix)
for i := 0; i < d.precision; i++ {
sb.WriteString("0")
}
return sb.String()
}