From bc540c66534bd10b0c95b6c2d2147185b1561189 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Sat, 9 Nov 2024 23:02:59 +0100 Subject: [PATCH] Simplify DateTimeType handling for Amazon DynamoDB Signed-off-by: Jacob Laursen --- .../dynamodb/internal/AbstractDynamoDBItem.java | 12 +++++------- .../AbstractDynamoDBItemSerializationTest.java | 16 +++++++--------- .../internal/AbstractTwoItemIntegrationTest.java | 16 ++++++++-------- .../internal/DateTimeItemIntegrationTest.java | 7 ++----- 4 files changed, 22 insertions(+), 29 deletions(-) diff --git a/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItem.java b/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItem.java index 10701a226df..0161572293f 100644 --- a/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItem.java +++ b/bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItem.java @@ -292,9 +292,8 @@ public abstract class AbstractDynamoDBItem implements DynamoDBItem { } else if (item instanceof ContactItem) { return new DynamoDBBigDecimalItem(name, convert(state, DecimalType.class).toBigDecimal(), time, expireDays); } else if (item instanceof DateTimeItem) { - return new DynamoDBStringItem(name, - ZONED_DATE_TIME_CONVERTER_STRING.toString(((DateTimeType) state).getZonedDateTime()), time, - expireDays); + return new DynamoDBStringItem(name, ZONED_DATE_TIME_CONVERTER_STRING + .toString(((DateTimeType) state).getZonedDateTime(ZoneId.systemDefault())), time, expireDays); } else if (item instanceof ImageItem) { throw new IllegalArgumentException("Unsupported item " + item.getClass().getSimpleName()); } else if (item instanceof LocationItem) { @@ -333,7 +332,8 @@ public abstract class AbstractDynamoDBItem implements DynamoDBItem { return new DynamoDBStringItem(name, stringType.toString(), time, expireDays); } else if (state instanceof DateTimeType dateType) { return new DynamoDBStringItem(name, - ZONED_DATE_TIME_CONVERTER_STRING.toString(dateType.getZonedDateTime()), time, expireDays); + ZONED_DATE_TIME_CONVERTER_STRING.toString(dateType.getZonedDateTime(ZoneId.systemDefault())), + time, expireDays); } else { throw new IllegalStateException( String.format("Unexpected state type %s with StringItem", state.getClass().getSimpleName())); @@ -386,9 +386,7 @@ public abstract class AbstractDynamoDBItem implements DynamoDBItem { try { // Parse ZoneDateTime from string. DATEFORMATTER assumes UTC in case it is not clear // from the string (should be). - // We convert to default/local timezone for user convenience (e.g. display) - return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState) - .withZoneSameInstant(ZoneId.systemDefault())); + return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState)); } catch (DateTimeParseException e) { logger.warn("Failed to parse {} as date. Outputting UNDEF instead", stringState); return UnDefType.UNDEF; diff --git a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItemSerializationTest.java b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItemSerializationTest.java index 7475d703a32..766017f4040 100644 --- a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItemSerializationTest.java +++ b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItemSerializationTest.java @@ -20,7 +20,6 @@ import java.time.Instant; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Objects; -import java.util.TimeZone; import org.eclipse.jdt.annotation.NonNullByDefault; import org.junit.jupiter.params.ParameterizedTest; @@ -148,10 +147,10 @@ public class AbstractDynamoDBItemSerializationTest { @CsvSource({ "true", "false" }) public void testDateTimeTypeWithDateTimeItem(boolean legacy) throws IOException { GenericItem item = new DateTimeItem("foo"); - ZonedDateTime zdt = ZonedDateTime.parse("2016-05-01T13:46:00.050Z"); - DynamoDBItem dbitem = testSerializationToDTO(legacy, item, new DateTimeType(zdt.toString()), + Instant instant = Instant.parse("2016-05-01T13:46:00.050Z"); + DynamoDBItem dbitem = testSerializationToDTO(legacy, item, new DateTimeType(instant), "2016-05-01T13:46:00.050Z"); - testAsHistoricGeneric(dbitem, item, new DateTimeType(zdt.withZoneSameInstant(ZoneId.systemDefault()))); + testAsHistoricGeneric(dbitem, item, new DateTimeType(instant)); } @ParameterizedTest @@ -159,7 +158,7 @@ public class AbstractDynamoDBItemSerializationTest { public void testDateTimeTypeWithStringItem(boolean legacy) throws IOException { GenericItem item = new StringItem("foo"); DynamoDBItem dbitem = testSerializationToDTO(legacy, item, - new DateTimeType(ZonedDateTime.parse("2016-05-01T13:46:00.050Z")), "2016-05-01T13:46:00.050Z"); + new DateTimeType(Instant.parse("2016-05-01T13:46:00.050Z")), "2016-05-01T13:46:00.050Z"); testAsHistoricGeneric(dbitem, item, new StringType("2016-05-01T13:46:00.050Z")); } @@ -167,10 +166,10 @@ public class AbstractDynamoDBItemSerializationTest { @CsvSource({ "true", "false" }) public void testDateTimeTypeLocalWithDateTimeItem(boolean legacy) throws IOException { GenericItem item = new DateTimeItem("foo"); - ZonedDateTime expectedZdt = Instant.ofEpochMilli(1468773487050L).atZone(ZoneId.systemDefault()); + Instant expectedInstant = Instant.ofEpochMilli(1468773487050L); DynamoDBItem dbitem = testSerializationToDTO(legacy, item, new DateTimeType("2016-07-17T19:38:07.050+0300"), "2016-07-17T16:38:07.050Z"); - testAsHistoricGeneric(dbitem, item, new DateTimeType(expectedZdt)); + testAsHistoricGeneric(dbitem, item, new DateTimeType(expectedInstant)); } @ParameterizedTest @@ -178,8 +177,7 @@ public class AbstractDynamoDBItemSerializationTest { public void testDateTimeTypeLocalWithStringItem(boolean legacy) throws IOException { GenericItem item = new StringItem("foo"); Instant instant = Instant.ofEpochMilli(1468773487050L); // GMT: Sun, 17 Jul 2016 16:38:07.050 GMT - ZonedDateTime zdt = instant.atZone(TimeZone.getTimeZone("GMT+03:00").toZoneId()); - DynamoDBItem dbitem = testSerializationToDTO(legacy, item, new DateTimeType(zdt), + DynamoDBItem dbitem = testSerializationToDTO(legacy, item, new DateTimeType(instant), "2016-07-17T16:38:07.050Z"); testAsHistoricGeneric(dbitem, item, new StringType("2016-07-17T16:38:07.050Z")); } diff --git a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractTwoItemIntegrationTest.java b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractTwoItemIntegrationTest.java index fb5a0b4c360..30ee4e27d0a 100644 --- a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractTwoItemIntegrationTest.java +++ b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractTwoItemIntegrationTest.java @@ -225,8 +225,8 @@ public abstract class AbstractTwoItemIntegrationTest extends BaseIntegrationTest HistoricItem actual1 = iterator.next(); assertFalse(iterator.hasNext()); assertStateEquals(getFirstItemState(), actual1.getState()); - assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant())); - assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant())); + assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant())); + assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant())); }); } @@ -246,8 +246,8 @@ public abstract class AbstractTwoItemIntegrationTest extends BaseIntegrationTest HistoricItem actual1 = iterator.next(); assertFalse(iterator.hasNext()); assertStateEquals(getFirstItemState(), actual1.getState()); - assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant())); - assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant())); + assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant())); + assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant())); }); } @@ -267,8 +267,8 @@ public abstract class AbstractTwoItemIntegrationTest extends BaseIntegrationTest HistoricItem actual1 = iterator.next(); assertFalse(iterator.hasNext()); assertStateEquals(getFirstItemState(), actual1.getState()); - assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant())); - assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant())); + assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant())); + assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant())); }); } @@ -304,8 +304,8 @@ public abstract class AbstractTwoItemIntegrationTest extends BaseIntegrationTest HistoricItem actual1 = iterator.next(); assertFalse(iterator.hasNext()); assertStateEquals(getFirstItemState(), actual1.getState()); - assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant())); - assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant())); + assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant())); + assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant())); }); } diff --git a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/DateTimeItemIntegrationTest.java b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/DateTimeItemIntegrationTest.java index 525ac84156f..75ad3a6127c 100644 --- a/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/DateTimeItemIntegrationTest.java +++ b/bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/DateTimeItemIntegrationTest.java @@ -12,7 +12,6 @@ */ package org.openhab.persistence.dynamodb.internal; -import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; @@ -71,23 +70,21 @@ public class DateTimeItemIntegrationTest extends AbstractTwoItemIntegrationTest @Override protected State getFirstItemState() { // The persistence converts to system default timezone - // Thus we need to convert here as well for comparison // In the logs: // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300 - return STATE1.toZone(ZoneId.systemDefault()); + return STATE1; } @Override protected State getSecondItemState() { // The persistence converts to system default timezone - // Thus we need to convert here as well for comparison // In the logs: // [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime // (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic // item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300 - return STATE2.toZone(ZoneId.systemDefault()); + return STATE2; } @Override