Skip to content

Commit 254f2f5

Browse files
jlaurlsiepel
authored andcommitted
Simplify DateTimeType handling for MongoDB
Signed-off-by: Jacob Laursen <[email protected]>
1 parent 2ded1d1 commit 254f2f5

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

bundles/org.openhab.persistence.mongodb/src/main/java/org/openhab/persistence/mongodb/internal/MongoDBTypeConversions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public static Object convertValue(State state) {
125125
HSBType.class, State::toString, //
126126
QuantityType.class, state -> ((QuantityType<?>) state).toBigDecimal().doubleValue(), //
127127
PercentType.class, state -> ((PercentType) state).intValue(), //
128-
DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime().toString(), //
128+
DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime(ZoneId.systemDefault()).toString(), //
129129
StringListType.class, State::toString, //
130130
DecimalType.class, state -> ((DecimalType) state).toBigDecimal().doubleValue(), //
131131
RawType.class, MongoDBTypeConversions::handleRawType//
@@ -237,7 +237,7 @@ private static State handleDateTimeItem(Item item, Document doc) {
237237
if (value instanceof String) {
238238
return new DateTimeType(ZonedDateTime.parse(doc.getString(MongoDBFields.FIELD_VALUE)));
239239
} else {
240-
return new DateTimeType(ZonedDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault()));
240+
return new DateTimeType(((Date) value).toInstant());
241241
}
242242
}
243243

bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/DataCreationHelper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.BufferedReader;
1919
import java.io.FileReader;
2020
import java.io.IOException;
21+
import java.time.Instant;
2122
import java.time.LocalDate;
2223
import java.time.ZonedDateTime;
2324
import java.util.ArrayList;
@@ -212,7 +213,7 @@ public static Stream<Arguments> provideOpenhabItemTypes() {
212213
Arguments.of(DataCreationHelper.createItem(RollershutterItem.class, "RollershutterItem",
213214
new PercentType(30))),
214215
Arguments.of(DataCreationHelper.createItem(DateTimeItem.class, "DateTimeItem",
215-
new DateTimeType(ZonedDateTime.now()))),
216+
new DateTimeType(Instant.now()))),
216217
Arguments.of(DataCreationHelper.createItem(ColorItem.class, "ColorItem", new HSBType("180,100,100"))),
217218
Arguments.of(
218219
DataCreationHelper.createItem(LocationItem.class, "LocationItem", new PointType("51.0,0.0"))),
@@ -397,7 +398,7 @@ private static Object convertValue(State state) {
397398
value = type.toBigDecimal().doubleValue();
398399
} else if (state instanceof DateTimeType) {
399400
DateTimeType type = (DateTimeType) state;
400-
value = Date.from(type.getZonedDateTime().toInstant());
401+
value = Date.from(type.getInstant());
401402
} else if (state instanceof DecimalType) {
402403
DecimalType type = (DecimalType) state;
403404
value = type.toBigDecimal().doubleValue();

bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/MongoDBPersistenceServiceTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import de.bwaldvogel.mongo.backend.memory.MemoryBackend;
6060

6161
/**
62-
* This is the implementation of the test for MongoDB {@link PersistenceService}.
62+
* This is the implementation of the test for MongoDB {@link org.openhab.core.persistence.PersistenceService}.
6363
*
6464
* @author René Ulbricht - Initial contribution
6565
*/
@@ -699,8 +699,9 @@ public void testOldDataQueryAllOpenhabItemTypesSingleCollection(GenericItem item
699699

700700
if (item instanceof DateTimeItem) {
701701
// verify just the date part
702-
assertEquals(((DateTimeType) item.getState()).getZonedDateTime().toLocalDate(),
703-
((DateTimeType) result.iterator().next().getState()).getZonedDateTime().toLocalDate());
702+
assertEquals(((DateTimeType) item.getState()).getZonedDateTime(ZoneId.systemDefault()).toLocalDate(),
703+
((DateTimeType) result.iterator().next().getState()).getZonedDateTime(ZoneId.systemDefault())
704+
.toLocalDate());
704705
} else {
705706
VerificationHelper.verifyQueryResult(result, item.getState());
706707
}

bundles/org.openhab.persistence.mongodb/src/test/java/org/openhab/persistence/mongodb/internal/VerificationHelper.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
1616
import static org.junit.jupiter.api.Assertions.assertNotNull;
1717

18+
import java.time.ZoneId;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021
import java.util.Map;
@@ -176,7 +177,8 @@ private static Pair<Object, Object> handleDecimalType(Object ev, Document doc) {
176177

177178
private static Pair<Object, Object> handleDateTimeType(Object ev, Document doc) {
178179
String value = doc.getString(MongoDBFields.FIELD_VALUE);
179-
return Pair.of(((DateTimeType) ev).getZonedDateTime().toString(), value != null ? value : new Object());
180+
return Pair.of(((DateTimeType) ev).getZonedDateTime(ZoneId.systemDefault()).toString(),
181+
value != null ? value : new Object());
180182
}
181183

182184
private static Pair<Object, Object> handlePercentType(Object ev, Document doc) {

0 commit comments

Comments
 (0)