forked from snowflakedb/gosnowflake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.go
150 lines (140 loc) · 4.38 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
// Copyright (c) 2024 Snowflake Computing Inc. All rights reserved.
package gosnowflake
import (
"errors"
"regexp"
"strconv"
"strings"
"time"
)
var incorrectSecondsFractionRegex = regexp.MustCompile(`[^.,]FF`)
var correctSecondsFractionRegex = regexp.MustCompile(`FF(?P<fraction>\d?)`)
type formatReplacement struct {
input string
output string
}
var formatReplacements = []formatReplacement{
{input: "YYYY", output: "2006"},
{input: "YY", output: "06"},
{input: "MMMM", output: "January"},
{input: "MM", output: "01"},
{input: "MON", output: "Jan"},
{input: "DD", output: "02"},
{input: "DY", output: "Mon"},
{input: "HH24", output: "15"},
{input: "HH12", output: "03"},
{input: "AM", output: "PM"},
{input: "MI", output: "04"},
{input: "SS", output: "05"},
{input: "TZH", output: "Z07"},
{input: "TZM", output: "00"},
}
func timeToString(t time.Time, dateTimeType string, params map[string]*string) (string, error) {
sfFormat, err := dateTimeInputFormatByType(dateTimeType, params)
if err != nil {
return "", err
}
goFormat, err := snowflakeFormatToGoFormat(sfFormat)
if err != nil {
return "", err
}
return t.Format(goFormat), nil
}
func snowflakeFormatToGoFormat(sfFormat string) (string, error) {
res := sfFormat
for _, replacement := range formatReplacements {
res = strings.Replace(res, replacement.input, replacement.output, -1)
}
if incorrectSecondsFractionRegex.MatchString(res) {
return "", errors.New("incorrect second fraction - golang requires fraction to be preceded by comma or decimal point")
}
for {
submatch := correctSecondsFractionRegex.FindStringSubmatch(res)
if submatch == nil {
break
}
fractionNumbers := 9
if submatch[1] != "" {
var err error
fractionNumbers, err = strconv.Atoi(submatch[1])
if err != nil {
return "", err
}
}
res = strings.Replace(res, submatch[0], strings.Repeat("0", fractionNumbers), -1)
}
return res, nil
}
func dateTimeOutputFormatByType(dateTimeType string, params map[string]*string) (string, error) {
paramsMutex.Lock()
defer paramsMutex.Unlock()
var format *string
switch strings.ToLower(dateTimeType) {
case "date":
format = params["date_output_format"]
case "time":
format = params["time_output_format"]
case "timestamp_ltz":
format = params["timestamp_ltz_output_format"]
if format == nil || *format == "" {
format = params["timestamp_output_format"]
}
case "timestamp_tz":
format = params["timestamp_tz_output_format"]
if format == nil || *format == "" {
format = params["timestamp_output_format"]
}
case "timestamp_ntz":
format = params["timestamp_ntz_output_format"]
if format == nil || *format == "" {
format = params["timestamp_output_format"]
}
}
if format != nil {
return *format, nil
}
return "", errors.New("not known output format parameter for " + dateTimeType)
}
func dateTimeInputFormatByType(dateTimeType string, params map[string]*string) (string, error) {
paramsMutex.Lock()
defer paramsMutex.Unlock()
var format *string
switch strings.ToLower(dateTimeType) {
case "date":
if format = params["date_input_format"]; format == nil || *format == "" {
format = params["date_output_format"]
}
case "time":
if format = params["time_input_format"]; format == nil || *format == "" {
format = params["time_output_format"]
}
case "timestamp_ltz":
if format = params["timestamp_ltz_input_format"]; format == nil || *format == "" {
if format = params["timestamp_input_format"]; format == nil || *format == "" {
if format = params["timestamp_ltz_output_format"]; format == nil || *format == "" {
format = params["timestamp_output_format"]
}
}
}
case "timestamp_tz":
if format = params["timestamp_tz_input_format"]; format == nil || *format == "" {
if format = params["timestamp_input_format"]; format == nil || *format == "" {
if format = params["timestamp_tz_output_format"]; format == nil || *format == "" {
format = params["timestamp_output_format"]
}
}
}
case "timestamp_ntz":
if format = params["timestamp_ntz_input_format"]; format == nil || *format == "" {
if format = params["timestamp_input_format"]; format == nil || *format == "" {
if format = params["timestamp_ntz_output_format"]; format == nil || *format == "" {
format = params["timestamp_output_format"]
}
}
}
}
if format != nil {
return *format, nil
}
return "", errors.New("not known input format parameter for " + dateTimeType)
}