Skip to content

Commit

Permalink
[Debezium] Support ns precision (#919)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Sep 20, 2024
1 parent eb47992 commit 175e027
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 36 deletions.
18 changes: 10 additions & 8 deletions lib/debezium/converters/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
59 changes: 31 additions & 28 deletions lib/debezium/converters/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down

0 comments on commit 175e027

Please sign in to comment.