Skip to content

Commit bc540c6

Browse files
jlaurlsiepel
authored andcommitted
Simplify DateTimeType handling for Amazon DynamoDB
Signed-off-by: Jacob Laursen <[email protected]>
1 parent 6007d6b commit bc540c6

File tree

4 files changed

+22
-29
lines changed

4 files changed

+22
-29
lines changed

bundles/org.openhab.persistence.dynamodb/src/main/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItem.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,8 @@ public static DynamoDBItem<?> fromStateNew(Item item, ZonedDateTime time, @Nulla
292292
} else if (item instanceof ContactItem) {
293293
return new DynamoDBBigDecimalItem(name, convert(state, DecimalType.class).toBigDecimal(), time, expireDays);
294294
} else if (item instanceof DateTimeItem) {
295-
return new DynamoDBStringItem(name,
296-
ZONED_DATE_TIME_CONVERTER_STRING.toString(((DateTimeType) state).getZonedDateTime()), time,
297-
expireDays);
295+
return new DynamoDBStringItem(name, ZONED_DATE_TIME_CONVERTER_STRING
296+
.toString(((DateTimeType) state).getZonedDateTime(ZoneId.systemDefault())), time, expireDays);
298297
} else if (item instanceof ImageItem) {
299298
throw new IllegalArgumentException("Unsupported item " + item.getClass().getSimpleName());
300299
} else if (item instanceof LocationItem) {
@@ -333,7 +332,8 @@ public static DynamoDBItem<?> fromStateNew(Item item, ZonedDateTime time, @Nulla
333332
return new DynamoDBStringItem(name, stringType.toString(), time, expireDays);
334333
} else if (state instanceof DateTimeType dateType) {
335334
return new DynamoDBStringItem(name,
336-
ZONED_DATE_TIME_CONVERTER_STRING.toString(dateType.getZonedDateTime()), time, expireDays);
335+
ZONED_DATE_TIME_CONVERTER_STRING.toString(dateType.getZonedDateTime(ZoneId.systemDefault())),
336+
time, expireDays);
337337
} else {
338338
throw new IllegalStateException(
339339
String.format("Unexpected state type %s with StringItem", state.getClass().getSimpleName()));
@@ -386,9 +386,7 @@ public static DynamoDBItem<?> fromStateNew(Item item, ZonedDateTime time, @Nulla
386386
try {
387387
// Parse ZoneDateTime from string. DATEFORMATTER assumes UTC in case it is not clear
388388
// from the string (should be).
389-
// We convert to default/local timezone for user convenience (e.g. display)
390-
return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState)
391-
.withZoneSameInstant(ZoneId.systemDefault()));
389+
return new DateTimeType(ZONED_DATE_TIME_CONVERTER_STRING.transformTo(stringState));
392390
} catch (DateTimeParseException e) {
393391
logger.warn("Failed to parse {} as date. Outputting UNDEF instead", stringState);
394392
return UnDefType.UNDEF;

bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractDynamoDBItemSerializationTest.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.time.ZoneId;
2121
import java.time.ZonedDateTime;
2222
import java.util.Objects;
23-
import java.util.TimeZone;
2423

2524
import org.eclipse.jdt.annotation.NonNullByDefault;
2625
import org.junit.jupiter.params.ParameterizedTest;
@@ -148,38 +147,37 @@ public void testOpenClosedTypeWithContactItem(boolean legacy) throws IOException
148147
@CsvSource({ "true", "false" })
149148
public void testDateTimeTypeWithDateTimeItem(boolean legacy) throws IOException {
150149
GenericItem item = new DateTimeItem("foo");
151-
ZonedDateTime zdt = ZonedDateTime.parse("2016-05-01T13:46:00.050Z");
152-
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new DateTimeType(zdt.toString()),
150+
Instant instant = Instant.parse("2016-05-01T13:46:00.050Z");
151+
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new DateTimeType(instant),
153152
"2016-05-01T13:46:00.050Z");
154-
testAsHistoricGeneric(dbitem, item, new DateTimeType(zdt.withZoneSameInstant(ZoneId.systemDefault())));
153+
testAsHistoricGeneric(dbitem, item, new DateTimeType(instant));
155154
}
156155

157156
@ParameterizedTest
158157
@CsvSource({ "true", "false" })
159158
public void testDateTimeTypeWithStringItem(boolean legacy) throws IOException {
160159
GenericItem item = new StringItem("foo");
161160
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item,
162-
new DateTimeType(ZonedDateTime.parse("2016-05-01T13:46:00.050Z")), "2016-05-01T13:46:00.050Z");
161+
new DateTimeType(Instant.parse("2016-05-01T13:46:00.050Z")), "2016-05-01T13:46:00.050Z");
163162
testAsHistoricGeneric(dbitem, item, new StringType("2016-05-01T13:46:00.050Z"));
164163
}
165164

166165
@ParameterizedTest
167166
@CsvSource({ "true", "false" })
168167
public void testDateTimeTypeLocalWithDateTimeItem(boolean legacy) throws IOException {
169168
GenericItem item = new DateTimeItem("foo");
170-
ZonedDateTime expectedZdt = Instant.ofEpochMilli(1468773487050L).atZone(ZoneId.systemDefault());
169+
Instant expectedInstant = Instant.ofEpochMilli(1468773487050L);
171170
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new DateTimeType("2016-07-17T19:38:07.050+0300"),
172171
"2016-07-17T16:38:07.050Z");
173-
testAsHistoricGeneric(dbitem, item, new DateTimeType(expectedZdt));
172+
testAsHistoricGeneric(dbitem, item, new DateTimeType(expectedInstant));
174173
}
175174

176175
@ParameterizedTest
177176
@CsvSource({ "true", "false" })
178177
public void testDateTimeTypeLocalWithStringItem(boolean legacy) throws IOException {
179178
GenericItem item = new StringItem("foo");
180179
Instant instant = Instant.ofEpochMilli(1468773487050L); // GMT: Sun, 17 Jul 2016 16:38:07.050 GMT
181-
ZonedDateTime zdt = instant.atZone(TimeZone.getTimeZone("GMT+03:00").toZoneId());
182-
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new DateTimeType(zdt),
180+
DynamoDBItem<?> dbitem = testSerializationToDTO(legacy, item, new DateTimeType(instant),
183181
"2016-07-17T16:38:07.050Z");
184182
testAsHistoricGeneric(dbitem, item, new StringType("2016-07-17T16:38:07.050Z"));
185183
}

bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/AbstractTwoItemIntegrationTest.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,8 @@ public void testQueryUsingNameAndStartAndEndWithNEQOperator() {
225225
HistoricItem actual1 = iterator.next();
226226
assertFalse(iterator.hasNext());
227227
assertStateEquals(getFirstItemState(), actual1.getState());
228-
assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant()));
229-
assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant()));
228+
assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant()));
229+
assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant()));
230230
});
231231
}
232232

@@ -246,8 +246,8 @@ public void testQueryUsingNameAndStartAndEndWithEQOperator() {
246246
HistoricItem actual1 = iterator.next();
247247
assertFalse(iterator.hasNext());
248248
assertStateEquals(getFirstItemState(), actual1.getState());
249-
assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant()));
250-
assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant()));
249+
assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant()));
250+
assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant()));
251251
});
252252
}
253253

@@ -267,8 +267,8 @@ public void testQueryUsingNameAndStartAndEndWithLTOperator() {
267267
HistoricItem actual1 = iterator.next();
268268
assertFalse(iterator.hasNext());
269269
assertStateEquals(getFirstItemState(), actual1.getState());
270-
assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant()));
271-
assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant()));
270+
assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant()));
271+
assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant()));
272272
});
273273
}
274274

@@ -304,8 +304,8 @@ public void testQueryUsingNameAndStartAndEndWithLTEOperator() {
304304
HistoricItem actual1 = iterator.next();
305305
assertFalse(iterator.hasNext());
306306
assertStateEquals(getFirstItemState(), actual1.getState());
307-
assertTrue(actual1.getTimestamp().toInstant().isBefore(afterStore1.toInstant()));
308-
assertTrue(actual1.getTimestamp().toInstant().isAfter(beforeStore.toInstant()));
307+
assertTrue(actual1.getInstant().isBefore(afterStore1.toInstant()));
308+
assertTrue(actual1.getInstant().isAfter(beforeStore.toInstant()));
309309
});
310310
}
311311

bundles/org.openhab.persistence.dynamodb/src/test/java/org/openhab/persistence/dynamodb/internal/DateTimeItemIntegrationTest.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313
package org.openhab.persistence.dynamodb.internal;
1414

15-
import java.time.ZoneId;
1615
import java.time.ZoneOffset;
1716
import java.time.ZonedDateTime;
1817

@@ -71,23 +70,21 @@ protected String getItemName() {
7170
@Override
7271
protected State getFirstItemState() {
7372
// The persistence converts to system default timezone
74-
// Thus we need to convert here as well for comparison
7573
// In the logs:
7674
// [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
7775
// (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
7876
// item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
79-
return STATE1.toZone(ZoneId.systemDefault());
77+
return STATE1;
8078
}
8179

8280
@Override
8381
protected State getSecondItemState() {
8482
// The persistence converts to system default timezone
85-
// Thus we need to convert here as well for comparison
8683
// In the logs:
8784
// [main] TRACE org.openhab.persistence.dynamodb.internal.DynamoDBPersistenceService - Dynamo item datetime
8885
// (Type=DateTimeItem, State=2016-06-15T16:00:00.123+0000, Label=null, Category=null) converted to historic
8986
// item: datetime: 2020-11-28T11:29:54.326Z: 2016-06-15T19:00:00.123+0300
90-
return STATE2.toZone(ZoneId.systemDefault());
87+
return STATE2;
9188
}
9289

9390
@Override

0 commit comments

Comments
 (0)