-
Notifications
You must be signed in to change notification settings - Fork 504
/
datetime.go
68 lines (57 loc) · 1.57 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
package types
import (
"fmt"
"strconv"
"strings"
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"github.com/ClickHouse/clickhouse-go/v2/lib/timezone"
"github.com/apache/arrow/go/v16/arrow"
)
func dateTimeType(name string, col *column.DateTime) (*arrow.Field, error) {
timeZone, err := getTimeZone(params(col.Type()))
if err != nil {
return nil, err
}
return &arrow.Field{Name: name, Type: &arrow.TimestampType{Unit: arrow.Second, TimeZone: timeZone}}, nil
}
func dateTime64Type(name string, col *column.DateTime64) (*arrow.Field, error) {
// need to parse
params := strings.SplitN(params(col.Type()), ",", 2)
precision, err := strconv.Atoi(params[0])
if err != nil {
return nil, err
}
var unit arrow.TimeUnit
switch {
case precision == 0:
unit = arrow.Second
case precision <= 3:
// This is the same as arrow.DATE64, so we need to canonize the schema
unit = arrow.Millisecond
case precision <= 6:
unit = arrow.Microsecond
case precision <= 9:
unit = arrow.Nanosecond
default:
return nil, fmt.Errorf("unsupported DateTime64 precision: %d (supported values: 0-9)", precision)
}
var timeZone string
if len(params) > 1 {
timeZone, err = getTimeZone(params[1])
if err != nil {
return nil, err
}
}
return &arrow.Field{Name: name, Type: &arrow.TimestampType{Unit: unit, TimeZone: timeZone}}, nil
}
func getTimeZone(param string) (string, error) {
tzName := strings.Trim(strings.TrimSpace(param), "'")
if len(tzName) == 0 {
return "", nil
}
loc, err := timezone.Load(tzName)
if err != nil {
return "", err
}
return loc.String(), nil
}