-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathdatetime.go
More file actions
145 lines (136 loc) · 4.35 KB
/
Copy pathdatetime.go
File metadata and controls
145 lines (136 loc) · 4.35 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
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, sp *syncParams) (string, error) {
sfFormat, err := dateTimeInputFormatByType(dateTimeType, sp)
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.ReplaceAll(res, replacement.input, replacement.output)
}
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.ReplaceAll(res, submatch[0], strings.Repeat("0", fractionNumbers))
}
return res, nil
}
func dateTimeOutputFormatByType(dateTimeType string, sp *syncParams) (string, error) {
var format *string
switch strings.ToLower(dateTimeType) {
case "date":
format, _ = sp.get("date_output_format")
case "time":
format, _ = sp.get("time_output_format")
case "timestamp_ltz":
format, _ = sp.get("timestamp_ltz_output_format")
if format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
case "timestamp_tz":
format, _ = sp.get("timestamp_tz_output_format")
if format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
case "timestamp_ntz":
format, _ = sp.get("timestamp_ntz_output_format")
if format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
}
if format != nil {
return *format, nil
}
return "", errors.New("not known output format parameter for " + dateTimeType)
}
func dateTimeInputFormatByType(dateTimeType string, sp *syncParams) (string, error) {
var format *string
var ok bool
switch strings.ToLower(dateTimeType) {
case "date":
if format, ok = sp.get("date_input_format"); !ok || format == nil || *format == "" {
format, _ = sp.get("date_output_format")
}
case "time":
if format, ok = sp.get("time_input_format"); !ok || format == nil || *format == "" {
format, _ = sp.get("time_output_format")
}
case "timestamp_ltz":
if format, ok = sp.get("timestamp_ltz_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_ltz_output_format"); !ok || format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
}
}
case "timestamp_tz":
if format, ok = sp.get("timestamp_tz_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_tz_output_format"); !ok || format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
}
}
case "timestamp_ntz":
if format, ok = sp.get("timestamp_ntz_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_input_format"); !ok || format == nil || *format == "" {
if format, ok = sp.get("timestamp_ntz_output_format"); !ok || format == nil || *format == "" {
format, _ = sp.get("timestamp_output_format")
}
}
}
}
if format != nil {
return *format, nil
}
return "", errors.New("not known input format parameter for " + dateTimeType)
}