Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Oct 22, 2024
1 parent c4d9561 commit acacce0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 36 deletions.
35 changes: 11 additions & 24 deletions lib/debezium/converters/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,17 @@ func (m MicroTime) Convert(value any) (any, error) {
return ext.NewExtendedTime(time.UnixMicro(castedVal).In(time.UTC), ext.TimeKindType, m.layout()), nil
}

var SupportedDateTimeWithTimezoneFormats = []string{
"2006-01-02T15:04:05Z", // w/o fractional seconds
"2006-01-02T15:04:05.0Z", // 1 digit
"2006-01-02T15:04:05.00Z", // 2 digits
"2006-01-02T15:04:05.000Z", // 3 digits
"2006-01-02T15:04:05.0000Z", // 4 digits
"2006-01-02T15:04:05.00000Z", // 5 digits
"2006-01-02T15:04:05.000000Z", // 6 digits
"2006-01-02T15:04:05.0000000Z", // 7 digits
"2006-01-02T15:04:05.00000000Z", // 8 digits
"2006-01-02T15:04:05.000000000Z", // 9 digits
}

type ZonedTimestamp struct{}

func (ZonedTimestamp) ToKindDetails() typing.KindDetails {
return typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, "")
func (ZonedTimestamp) layout() string {
return "2006-01-02T15:04:05.999999999Z"
}

func (ZonedTimestamp) Convert(value any) (any, error) {
func (z ZonedTimestamp) ToKindDetails() typing.KindDetails {
return typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, z.layout())
}

func (z ZonedTimestamp) Convert(value any) (any, error) {
valString, isOk := value.(string)
if !isOk {
return nil, fmt.Errorf("expected string got '%v' with type %T", value, value)
Expand All @@ -102,16 +93,12 @@ func (ZonedTimestamp) Convert(value any) (any, error) {
}
}

var err error
var ts time.Time
for _, supportedFormat := range SupportedDateTimeWithTimezoneFormats {
ts, err = ext.ParseTimeExactMatch(supportedFormat, valString)
if err == nil {
return ext.NewExtendedTime(ts, ext.TimestampTZKindType, supportedFormat), nil
}
_time, err := time.Parse(z.layout(), valString)
if err != nil {
return nil, fmt.Errorf("failed to parse %q: %w", valString, err)
}

return nil, fmt.Errorf("failed to parse %q, err: %w", valString, err)
return ext.NewExtendedTime(_time, ext.TimestampTZKindType, z.layout()), nil
}

var SupportedTimeWithTimezoneFormats = []string{
Expand Down
32 changes: 21 additions & 11 deletions lib/debezium/converters/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,83 +32,93 @@ func TestZonedTimestamp_Convert(t *testing.T) {
// Valid
{
// No fractional seconds
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00Z")
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:12Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 000000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 12, 000000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:12Z", val.(*ext.ExtendedTime).String(""))
}
{
// 1 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 100000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 100000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.1Z", val.(*ext.ExtendedTime).String(""))
}
{
// 2 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 120000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 120000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.12Z", val.(*ext.ExtendedTime).String(""))
}
{
// 3 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123000000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.123Z", val.(*ext.ExtendedTime).String(""))
}
{
// 4 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1234Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123400000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123400000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.1234Z", val.(*ext.ExtendedTime).String(""))
}
{
// 5 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12345Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123450000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123450000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.12345Z", val.(*ext.ExtendedTime).String(""))
}
{
// 6 digits (microseconds)
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123456Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456000, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.123456Z", val.(*ext.ExtendedTime).String(""))
}
{
// 7 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.1234567Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456700, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.0000000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456700, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.1234567Z", val.(*ext.ExtendedTime).String(""))
}
{
// 8 digits
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.12345678Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456780, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.00000000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456780, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.12345678Z", val.(*ext.ExtendedTime).String(""))
}
{
// 9 digits (nanoseconds)
val, err := ZonedTimestamp{}.Convert("2025-09-13T00:00:00.123456789Z")
assert.NoError(t, err)

expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456789, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.000000000Z")
expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456789, time.UTC), ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z")
assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime))
assert.Equal(t, "2025-09-13T00:00:00.123456789Z", val.(*ext.ExtendedTime).String(""))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/debezium/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func TestField_ToKindDetails(t *testing.T) {
for _, dbzType := range []SupportedDebeziumType{ZonedTimestamp} {
kd, err := Field{DebeziumType: dbzType}.ToKindDetails()
assert.NoError(t, err)
assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, ""), kd)
assert.Equal(t, typing.NewExtendedTimeDetails(typing.ETime, ext.TimestampTZKindType, "2006-01-02T15:04:05.999999999Z"), kd)
}
}
{
Expand Down

0 comments on commit acacce0

Please sign in to comment.