Skip to content

Commit

Permalink
Do Time too.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jul 29, 2024
1 parent 079f6ef commit 0db1d07
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
3 changes: 1 addition & 2 deletions lib/debezium/converters/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func (Date) Convert(value any) (any, error) {
return nil, fmt.Errorf("expected int64 got '%v' with type %T", value, value)
}

unix := time.UnixMilli(0).In(time.UTC) // 1970-01-01
// Represents the number of days since the epoch.
return ext.NewExtendedTime(unix.AddDate(0, 0, int(valueInt64)), ext.DateKindType, ""), nil
return ext.NewExtendedTime(time.UnixMilli(0).In(time.UTC).AddDate(0, 0, int(valueInt64)), ext.DateKindType, ""), nil
}
16 changes: 16 additions & 0 deletions lib/debezium/converters/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ import (
"github.com/artie-labs/transfer/lib/typing/ext"
)

type Time struct{}

func (Time) ToKindDetails() typing.KindDetails {
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType)
}

func (Time) Convert(val any) (any, error) {
valInt64, isOk := val.(int64)
if isOk {
return nil, fmt.Errorf("expected int64 got '%v' with type %T", val, val)
}

// Represents the number of milliseconds past midnight, and does not include timezone information.
return ext.NewExtendedTime(time.UnixMilli(valInt64).In(time.UTC), ext.TimeKindType, ""), nil
}

type DateTimeWithTimezone struct{}

func (DateTimeWithTimezone) ToKindDetails() typing.KindDetails {
Expand Down
4 changes: 3 additions & 1 deletion lib/debezium/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func (f Field) ToValueConverter() converters.ValueConverter {
return converters.JSON{}
case Date, DateKafkaConnect:
return converters.Date{}
case Time, TimeKafkaConnect:
return converters.Time{}
}

return nil
Expand All @@ -112,7 +114,7 @@ func (f Field) ToKindDetails() typing.KindDetails {
switch f.DebeziumType {
case Timestamp, MicroTimestamp, NanoTimestamp, DateTimeKafkaConnect:
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.DateTimeKindType)
case Time, MicroTime, NanoTime, TimeKafkaConnect:
case MicroTime, NanoTime:
return typing.NewKindDetailsFromTemplate(typing.ETime, ext.TimeKindType)
case KafkaDecimalType:
scale, precisionPtr, err := f.GetScaleAndPrecision()
Expand Down
3 changes: 0 additions & 3 deletions lib/debezium/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ func FromDebeziumTypeToTime(supportedType SupportedDebeziumType, val int64) (*ex
case NanoTimestamp:
// Represents the number of nanoseconds past the epoch, and does not include timezone information.
extTime = ext.NewExtendedTime(time.UnixMicro(val/1_000).In(time.UTC), ext.DateTimeKindType, time.RFC3339Nano)
case Time, TimeKafkaConnect:
// Represents the number of milliseconds past midnight, and does not include timezone information.
extTime = ext.NewExtendedTime(time.UnixMilli(val).In(time.UTC), ext.TimeKindType, "")
case MicroTime:
// Represents the number of microseconds past midnight, and does not include timezone information.
extTime = ext.NewExtendedTime(time.UnixMicro(val).In(time.UTC), ext.TimeKindType, "")
Expand Down

0 comments on commit 0db1d07

Please sign in to comment.