-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
88 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package converters | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
) | ||
|
||
func ConvertDateTimeWithTimezone(value any) (*ext.ExtendedTime, error) { | ||
dtString, isOk := value.(string) | ||
if !isOk { | ||
return nil, fmt.Errorf("expected string got '%v' with type %T", value, value) | ||
} | ||
|
||
// We don't need to pass `additionalDateFormats` because this data type layout is standardized by Debezium | ||
extTime, err := ext.ParseExtendedDateTime(dtString, nil) | ||
if err == nil { | ||
return extTime, nil | ||
} | ||
|
||
// Check for negative years | ||
if strings.HasPrefix(dtString, "-") { | ||
return nil, nil | ||
} | ||
|
||
if parts := strings.Split(dtString, "-"); len(parts) == 3 { | ||
// Check if year exceeds 9999 | ||
if len(parts[0]) > 4 { | ||
return nil, nil | ||
} | ||
} | ||
|
||
return nil, fmt.Errorf("failed to parse %q, err: %w", dtString, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package converters | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/artie-labs/transfer/lib/typing/ext" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertDateTimeWithTimezone(t *testing.T) { | ||
{ | ||
// Invalid data | ||
_, err := ConvertDateTimeWithTimezone(123) | ||
assert.ErrorContains(t, err, "expected string got '123' with type int") | ||
} | ||
{ | ||
// Edge case (Year exceeds 9999) | ||
val, err := ConvertDateTimeWithTimezone("+275760-09-13T00:00:00.000000Z") | ||
assert.NoError(t, err) | ||
assert.Nil(t, val) | ||
} | ||
{ | ||
// Edge case (Negative year) | ||
val, err := ConvertDateTimeWithTimezone("-0999-10-10T10:10:10.000000Z") | ||
assert.NoError(t, err) | ||
assert.Nil(t, val) | ||
} | ||
{ | ||
// Valid | ||
val, err := ConvertDateTimeWithTimezone("2025-09-13T00:00:00.000000Z") | ||
assert.NoError(t, err) | ||
|
||
expectedExtTime := &ext.ExtendedTime{ | ||
Time: time.Date(2025, time.September, 13, 0, 0, 0, 0, time.UTC), | ||
NestedKind: ext.NestedKind{ | ||
Type: ext.DateTimeKindType, | ||
Format: "2006-01-02T15:04:05Z07:00", | ||
}, | ||
} | ||
|
||
assert.Equal(t, expectedExtTime, val) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters