From fcab8d4fc92b45b44ed10825d230359071ee3895 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Thu, 14 Nov 2024 22:23:26 +0100 Subject: [PATCH] Simplify DateTimeType handling for JDBC Signed-off-by: Jacob Laursen --- .../openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java | 5 ++--- .../persistence/jdbc/internal/db/JdbcBaseDAOTest.java | 6 ++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java index 177fbc291d7f0..20f945e32c28f 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAO.java @@ -639,8 +639,7 @@ protected ItemVO storeItemValueProvider(Item item, State itemState, ItemVO vo) { break; case "DATETIMEITEM": vo.setValueTypes(getSqlTypes().get(itemType), java.sql.Timestamp.class); - java.sql.Timestamp d = new java.sql.Timestamp( - ((DateTimeType) itemState).getZonedDateTime().toInstant().toEpochMilli()); + java.sql.Timestamp d = new java.sql.Timestamp(((DateTimeType) itemState).getInstant().toEpochMilli()); logger.debug("JDBC::storeItemValueProvider: DateTimeItem: '{}'", d); vo.setValue(d); break; @@ -684,7 +683,7 @@ protected State objectAsState(Item item, @Nullable Unit> u } return unit == null ? DecimalType.valueOf(objectAsString(v)) : QuantityType.valueOf(objectAsString(v)); } else if (item instanceof DateTimeItem) { - return new DateTimeType(objectAsInstant(v).atZone(ZoneId.systemDefault())); + return new DateTimeType(objectAsInstant(v)); } else if (item instanceof ColorItem) { return HSBType.valueOf(objectAsString(v)); } else if (item instanceof DimmerItem || item instanceof RollershutterItem) { diff --git a/bundles/org.openhab.persistence.jdbc/src/test/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAOTest.java b/bundles/org.openhab.persistence.jdbc/src/test/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAOTest.java index c72d932736698..69e8c9bc413ac 100644 --- a/bundles/org.openhab.persistence.jdbc/src/test/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAOTest.java +++ b/bundles/org.openhab.persistence.jdbc/src/test/java/org/openhab/persistence/jdbc/internal/db/JdbcBaseDAOTest.java @@ -127,13 +127,11 @@ private static Stream provideTestCasesForObjectAsStateValid() { LocalDateTime.parse("2021-02-01T23:30:02.049"), null, DateTimeType.valueOf("2021-02-01T23:30:02.049")), Arguments.of(new DateTimeItem("Long_DateTimeItem"), Long.valueOf("1612222202049"), null, - new DateTimeType(ZonedDateTime.ofInstant(Instant.parse("2021-02-01T23:30:02.049Z"), - ZoneId.systemDefault()))), + new DateTimeType(Instant.parse("2021-02-01T23:30:02.049Z"))), Arguments.of(new DateTimeItem("Date_DateTimeItem"), java.sql.Date.valueOf("2021-02-01"), null, DateTimeType.valueOf("2021-02-01T00:00:00.000")), Arguments.of(new DateTimeItem("Instant_DateTimeItem"), Instant.parse("2021-02-01T23:30:02.049Z"), null, - new DateTimeType(ZonedDateTime.ofInstant(Instant.parse("2021-02-01T23:30:02.049Z"), - ZoneId.systemDefault()))), + new DateTimeType(Instant.parse("2021-02-01T23:30:02.049Z"))), Arguments.of(new DateTimeItem("String_DateTimeItem"), "2021-02-01 23:30:02.049", null, DateTimeType.valueOf("2021-02-01T23:30:02.049"))); }