Skip to content

Commit 6cc5242

Browse files
Lexert19Lexert19
authored andcommitted
truncate seconds in toZoneOffset for DB compatibility
1 parent 531249e commit 6cc5242

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/DateTimeUtils.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ public static ZonedDateTime convertToZonedDateTime(long timestamp, ZoneId zoneId
821821
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(timestamp), zoneId);
822822
}
823823

824+
/** Converts string to ZoneOffset. Truncates seconds for HH:mm database compatibility. */
824825
public static ZoneOffset toZoneOffset(String str, ZoneId zoneId) {
825826
if (str.endsWith("Z")) {
826827
return ZoneOffset.UTC;
@@ -834,7 +835,8 @@ public static ZoneOffset toZoneOffset(String str, ZoneId zoneId) {
834835
long millis = convertDatetimeStrToLong(str, ZoneOffset.UTC, 0, "ms");
835836
LocalDateTime localDateTime =
836837
LocalDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneOffset.UTC);
837-
return zoneId.getRules().getOffset(localDateTime);
838+
ZoneOffset offset = zoneId.getRules().getOffset(localDateTime);
839+
return ZoneOffset.ofTotalSeconds((offset.getTotalSeconds() / 60) * 60);
838840
}
839841

840842
public static ZonedDateTime convertMillsecondToZonedDateTime(long millisecond) {

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/DateTimeUtilsTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,4 +417,18 @@ public void testToZoneOffsetWithBrokenDate() {
417417
DateTimeUtils.toZoneOffset("2024-12-31 10", zoneId);
418418
});
419419
}
420+
421+
@Test
422+
public void testToZoneOffsetHistoricalLMT() {
423+
ZoneId zoneId = ZoneId.of("Europe/Warsaw");
424+
ZoneOffset offset = DateTimeUtils.toZoneOffset("0001-01-01 00:00:00", zoneId);
425+
Assert.assertEquals(ZoneOffset.ofHoursMinutes(1, 24), offset);
426+
427+
ZoneId shanghaiId = ZoneId.of("Asia/Shanghai");
428+
ZoneOffset shanghaiOffset = DateTimeUtils.toZoneOffset("0001-01-01 00:00:00", shanghaiId);
429+
Assert.assertEquals(ZoneOffset.ofHoursMinutes(8, 5), shanghaiOffset);
430+
431+
ZoneId utcId = ZoneId.of("UTC");
432+
Assert.assertEquals(ZoneOffset.UTC, DateTimeUtils.toZoneOffset("0001-01-01 00:00:00", utcId));
433+
}
420434
}

0 commit comments

Comments
 (0)