Skip to content

Commit

Permalink
Additional time layout (#1043)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Nov 14, 2024
1 parent 123b734 commit 2f5ac48
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions lib/typing/ext/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ func TestParseTimestampTZFromAny(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "2024-09-19T16:05:18.630000002Z", value.Format(time.RFC3339Nano))
}
{
// Another string variant
value, err := ParseTimestampTZFromAny("2023-07-20T11:01:33.159+00:00")
assert.NoError(t, err)
assert.Equal(t, "2023-07-20T11:01:33.159Z", value.Format(time.RFC3339Nano))
}
}

func TestParseTimestampNTZFromAny(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions lib/typing/ext/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const (

var supportedDateTimeLayouts = []string{
// RFC 3339
time.RFC3339,
time.RFC3339Nano,
RFC3339Millisecond,
RFC3339Microsecond,
RFC3339Nanosecond,
time.RFC3339Nano,
// Others
"2006-01-02T15:04:05.999999999-07:00",
time.Layout,
time.ANSIC,
time.UnixDate,
Expand All @@ -25,7 +26,6 @@ var supportedDateTimeLayouts = []string{
time.RFC850,
time.RFC1123,
time.RFC1123Z,
time.RFC3339,
}

var supportedDateFormats = []string{
Expand Down
13 changes: 13 additions & 0 deletions lib/typing/ext/variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ext

import "testing"

func TestSupportedDateTimeLayoutsUniqueness(t *testing.T) {
layouts := make(map[string]bool)
for _, layout := range supportedDateTimeLayouts {
if _, ok := layouts[layout]; ok {
t.Errorf("layout %q is duplicated", layout)
}
layouts[layout] = true
}
}
8 changes: 4 additions & 4 deletions lib/typing/values/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,28 @@ func ToString(colVal any, colKind typing.KindDetails) (string, error) {
case typing.Date.Kind:
_time, err := ext.ParseDateFromAny(colVal)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err)
return "", fmt.Errorf("failed to cast colVal as date, colVal: '%v', err: %w", colVal, err)
}

return _time.Format(ext.PostgresDateFormat), nil
case typing.Time.Kind:
_time, err := ext.ParseTimeFromAny(colVal)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err)
return "", fmt.Errorf("failed to cast colVal as time, colVal: '%v', err: %w", colVal, err)
}

return _time.Format(ext.PostgresTimeFormatNoTZ), nil
case typing.TimestampNTZ.Kind:
_time, err := ext.ParseTimestampNTZFromAny(colVal)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err)
return "", fmt.Errorf("failed to cast colVal as timestampNTZ, colVal: '%v', err: %w", colVal, err)
}

return _time.Format(ext.RFC3339NoTZ), nil
case typing.TimestampTZ.Kind:
_time, err := ext.ParseTimestampTZFromAny(colVal)
if err != nil {
return "", fmt.Errorf("failed to cast colVal as time.Time, colVal: '%v', err: %w", colVal, err)
return "", fmt.Errorf("failed to cast colVal as timestampTZ, colVal: '%v', err: %w", colVal, err)
}

return _time.Format(time.RFC3339Nano), nil
Expand Down

0 comments on commit 2f5ac48

Please sign in to comment.