From 58353e872234e32f9db2a20af8da769cad4dae0e Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Mon, 2 Dec 2024 21:46:10 +0100 Subject: [PATCH] Deprecate DateTimeType.getZonedDateTime and introduce new overload with ZoneId Signed-off-by: Jacob Laursen --- .../automation/rest/internal/RuleResource.java | 2 +- .../module/handler/DateTimeTriggerHandler.java | 2 +- .../persistence/PersistenceResource.java | 2 +- .../core/library/types/DateTimeType.java | 18 +++++++++++++++--- .../core/library/types/DateTimeTypeTest.java | 3 +++ 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java b/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java index 44812e50efd..70fb225820a 100644 --- a/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java +++ b/bundles/org.openhab.core.automation.rest/src/main/java/org/openhab/core/automation/rest/internal/RuleResource.java @@ -441,7 +441,7 @@ private ZonedDateTime parseTime(@Nullable String sTime, Supplier return defaultSupplier.get(); } final DateTimeType dateTime = new DateTimeType(sTime); - return dateTime.getInstant().atZone(timeZoneProvider.getTimeZone()); + return dateTime.getZonedDateTime(timeZoneProvider.getTimeZone()); } @GET diff --git a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java index cf1e650984e..a31c72a5a2b 100644 --- a/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java +++ b/bundles/org.openhab.core.automation/src/main/java/org/openhab/core/automation/internal/module/handler/DateTimeTriggerHandler.java @@ -178,7 +178,7 @@ private void process(Type value) { cronExpression = CronAdjuster.REBOOT; } else if (value instanceof DateTimeType dateTimeType) { boolean itemIsTimeOnly = dateTimeType.toString().startsWith("1970-01-01T"); - cronExpression = dateTimeType.getInstant().atZone(ZoneId.systemDefault()).plusSeconds(offset.longValue()) + cronExpression = dateTimeType.getZonedDateTime(ZoneId.systemDefault()).plusSeconds(offset.longValue()) .format(timeOnly || itemIsTimeOnly ? CRON_TIMEONLY_FORMATTER : CRON_FORMATTER); startScheduler(); } else { diff --git a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java index 7678438b39b..8564a8333bf 100644 --- a/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java +++ b/bundles/org.openhab.core.io.rest.core/src/main/java/org/openhab/core/io/rest/core/internal/persistence/PersistenceResource.java @@ -340,7 +340,7 @@ public Response httpPutPersistenceItemData(@Context HttpHeaders headers, private ZonedDateTime convertTime(String sTime) { DateTimeType dateTime = new DateTimeType(sTime); - return dateTime.getInstant().atZone(timeZoneProvider.getTimeZone()); + return dateTime.getZonedDateTime(timeZoneProvider.getTimeZone()); } private Response getItemHistoryDTO(@Nullable String serviceId, String itemName, @Nullable String timeBegin, diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java index addc9db94a4..3c499481b12 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java @@ -142,13 +142,25 @@ public DateTimeType(String zonedValue) { } /** - * Get object represented as a {@link ZonedDateTime} with system - * default time-zone applied + * @deprecated + * Get object represented as a {@link ZonedDateTime} with system + * default time-zone applied * * @return a {@link ZonedDateTime} representation of the object */ + @Deprecated public ZonedDateTime getZonedDateTime() { - return instant.atZone(ZoneId.systemDefault()); + return getZonedDateTime(ZoneId.systemDefault()); + } + + /** + * Get object represented as a {@link ZonedDateTime} with the + * the provided time-zone applied + * + * @return a {@link ZonedDateTime} representation of the object + */ + public ZonedDateTime getZonedDateTime(ZoneId zoneId) { + return instant.atZone(zoneId); } /** diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java index 40266d00ba3..f999114b838 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java @@ -285,15 +285,18 @@ public void zonedParsingTest() { DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); DateTimeType dt2 = new DateTimeType("2019-06-12T17:30:00+0000"); DateTimeType dt3 = new DateTimeType("2019-06-12T19:30:00+0200"); + DateTimeType dt4 = new DateTimeType("2019-06-12T19:30:00+0200"); assertThat(dt1, is(dt2)); ZonedDateTime zdt1 = dt1.getZonedDateTime(); ZonedDateTime zdt2 = dt2.getZonedDateTime(); ZonedDateTime zdt3 = dt3.getZonedDateTime(); + ZonedDateTime zdt4 = dt4.getZonedDateTime(ZoneId.of("UTC")); assertThat(zdt1.getZone(), is(zdt2.getZone())); assertThat(zdt1, is(zdt2)); assertThat(zdt1, is(zdt3.withZoneSameInstant(zdt1.getZone()))); assertThat(zdt2, is(zdt3.withZoneSameInstant(zdt2.getZone()))); + assertThat(zdt1, is(zdt4)); } @Test