github.com/snowflakedb/gosnowflake@v1.9.0/datetime.go (about) 1 // Copyright (c) 2024 Snowflake Computing Inc. All rights reserved. 2 3 package gosnowflake 4 5 import ( 6 "errors" 7 "regexp" 8 "strconv" 9 "strings" 10 ) 11 12 var incorrectSecondsFractionRegex = regexp.MustCompile(`[^.,]FF`) 13 var correctSecondsFractionRegex = regexp.MustCompile(`FF(?P<fraction>\d?)`) 14 15 type formatReplacement struct { 16 input string 17 output string 18 } 19 20 var formatReplacements = []formatReplacement{ 21 {input: "YYYY", output: "2006"}, 22 {input: "YY", output: "06"}, 23 {input: "MMMM", output: "January"}, 24 {input: "MM", output: "01"}, 25 {input: "MON", output: "Jan"}, 26 {input: "DD", output: "02"}, 27 {input: "DY", output: "Mon"}, 28 {input: "HH24", output: "15"}, 29 {input: "HH12", output: "03"}, 30 {input: "AM", output: "PM"}, 31 {input: "MI", output: "04"}, 32 {input: "SS", output: "05"}, 33 {input: "TZH", output: "Z07"}, 34 {input: "TZM", output: "00"}, 35 } 36 37 func snowflakeFormatToGoFormat(sfFormat string) (string, error) { 38 res := sfFormat 39 for _, replacement := range formatReplacements { 40 res = strings.Replace(res, replacement.input, replacement.output, -1) 41 } 42 43 if incorrectSecondsFractionRegex.MatchString(res) { 44 return "", errors.New("incorrect second fraction - golang requires fraction to be preceded by comma or decimal point") 45 } 46 for { 47 submatch := correctSecondsFractionRegex.FindStringSubmatch(res) 48 if submatch == nil { 49 break 50 } 51 fractionNumbers := 9 52 if submatch[1] != "" { 53 var err error 54 fractionNumbers, err = strconv.Atoi(submatch[1]) 55 if err != nil { 56 return "", err 57 } 58 } 59 res = strings.Replace(res, submatch[0], strings.Repeat("0", fractionNumbers), -1) 60 } 61 return res, nil 62 }