From e85f9f2b44d824a71b598ca9d3ceade80b4f8ed1 Mon Sep 17 00:00:00 2001 From: Robin Tang Date: Thu, 19 Sep 2024 17:02:53 -0700 Subject: [PATCH] Support upto nanoseconds. --- lib/debezium/converters/time.go | 18 +++++---- lib/debezium/converters/time_test.go | 59 +++++++++++++++------------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/lib/debezium/converters/time.go b/lib/debezium/converters/time.go index 7adec53a9..9c0536f40 100644 --- a/lib/debezium/converters/time.go +++ b/lib/debezium/converters/time.go @@ -58,14 +58,16 @@ func (MicroTime) Convert(value any) (any, error) { } 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: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 DateTimeWithTimezone struct{} diff --git a/lib/debezium/converters/time_test.go b/lib/debezium/converters/time_test.go index 535c84735..7fc1e2b68 100644 --- a/lib/debezium/converters/time_test.go +++ b/lib/debezium/converters/time_test.go @@ -36,77 +36,80 @@ func TestConvertDateTimeWithTimezone(t *testing.T) { val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 000000000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 1 digits val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.1Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 100000000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.0Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 2 digits val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.12Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 120000000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.00Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 3 digits val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.123Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123000000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.000Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 4 digits val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.1234Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123400000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.0000Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 5 digits val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.12345Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123450000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.00000Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } { // 6 digits (microseconds) val, err := DateTimeWithTimezone{}.Convert("2025-09-13T00:00:00.123456Z") assert.NoError(t, err) - ts, isOk := val.(*ext.ExtendedTime) - assert.True(t, isOk) - expectedExtTime := ext.NewExtendedTime(time.Date(2025, time.September, 13, 0, 0, 0, 123456000, time.UTC), ext.TimestampTzKindType, "2006-01-02T15:04:05.000000Z") - assert.Equal(t, expectedExtTime, ts) + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + } + { + // 7 digits + val, err := DateTimeWithTimezone{}.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") + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + } + { + // 8 digits + val, err := DateTimeWithTimezone{}.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") + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) + } + { + // 9 digits (nanoseconds) + val, err := DateTimeWithTimezone{}.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") + assert.Equal(t, expectedExtTime, val.(*ext.ExtendedTime)) } } }