Skip to content

Commit

Permalink
Fix precision error for datetime (#2106)
Browse files Browse the repository at this point in the history
  • Loading branch information
Deep1998 authored Jan 6, 2025
1 parent a753748 commit 2ffbcce
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,10 @@ static String handleRecordFieldType(String fieldName, GenericRecord element, Sch
// Convert to timestamp string.
Long totalMicros = TimeUnit.DAYS.toMicros(Long.valueOf(element.get("date").toString()));
totalMicros += Long.valueOf(element.get("time").toString());
Instant timestamp = Instant.ofEpochSecond(TimeUnit.MICROSECONDS.toSeconds(totalMicros));
Instant timestamp =
Instant.ofEpochSecond(
TimeUnit.MICROSECONDS.toSeconds(totalMicros),
TimeUnit.MICROSECONDS.toNanos(totalMicros % TimeUnit.SECONDS.toMicros(1)));
return timestamp.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} else if (fieldSchema.getName().equals("interval")) {
// TODO: For MySQL, we ignore the months field. This might require source-specific handling
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,9 @@ public void testHandleRecordFieldType() {
result =
GenericRecordTypeConvertor.handleRecordFieldType(
"date_time_column",
AvroTestingHelper.createDatetimeRecord(738991, 48035000000L),
AvroTestingHelper.createDatetimeRecord(20091, 31703699206L),
AvroTestingHelper.DATETIME_SCHEMA);
assertEquals("Test datetime conversion: ", "3993-04-16T13:20:35Z", result);
assertEquals("Test datetime conversion: ", "2025-01-03T08:48:23.699206Z", result);

// Tests for interval type.
result =
Expand Down

0 comments on commit 2ffbcce

Please sign in to comment.