Skip to content

Commit

Permalink
Simplify DateTimeType handling for MongoDB
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Nov 27, 2024
1 parent fcab8d4 commit 0c11a90
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public static Object convertValue(State state) {
HSBType.class, State::toString, //
QuantityType.class, state -> ((QuantityType<?>) state).toBigDecimal().doubleValue(), //
PercentType.class, state -> ((PercentType) state).intValue(), //
DateTimeType.class, state -> ((DateTimeType) state).getZonedDateTime().toString(), //
DateTimeType.class, state -> ((DateTimeType) state).getInstant().atZone(ZoneId.systemDefault()).toString(), //
StringListType.class, State::toString, //
DecimalType.class, state -> ((DecimalType) state).toBigDecimal().doubleValue(), //
RawType.class, MongoDBTypeConversions::handleRawType//
Expand Down Expand Up @@ -237,7 +237,7 @@ private static State handleDateTimeItem(Item item, Document doc) {
if (value instanceof String) {
return new DateTimeType(ZonedDateTime.parse(doc.getString(MongoDBFields.FIELD_VALUE)));
} else {
return new DateTimeType(ZonedDateTime.ofInstant(((Date) value).toInstant(), ZoneId.systemDefault()));
return new DateTimeType(((Date) value).toInstant());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZonedDateTime;
import java.util.ArrayList;
Expand Down Expand Up @@ -212,7 +213,7 @@ public static Stream<Arguments> provideOpenhabItemTypes() {
Arguments.of(DataCreationHelper.createItem(RollershutterItem.class, "RollershutterItem",
new PercentType(30))),
Arguments.of(DataCreationHelper.createItem(DateTimeItem.class, "DateTimeItem",
new DateTimeType(ZonedDateTime.now()))),
new DateTimeType(Instant.now()))),
Arguments.of(DataCreationHelper.createItem(ColorItem.class, "ColorItem", new HSBType("180,100,100"))),
Arguments.of(
DataCreationHelper.createItem(LocationItem.class, "LocationItem", new PointType("51.0,0.0"))),
Expand Down Expand Up @@ -397,7 +398,7 @@ private static Object convertValue(State state) {
value = type.toBigDecimal().doubleValue();
} else if (state instanceof DateTimeType) {
DateTimeType type = (DateTimeType) state;
value = Date.from(type.getZonedDateTime().toInstant());
value = Date.from(type.getInstant());
} else if (state instanceof DecimalType) {
DecimalType type = (DecimalType) state;
value = type.toBigDecimal().doubleValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,9 @@ public void testOldDataQueryAllOpenhabItemTypesSingleCollection(GenericItem item

if (item instanceof DateTimeItem) {
// verify just the date part
assertEquals(((DateTimeType) item.getState()).getZonedDateTime().toLocalDate(),
((DateTimeType) result.iterator().next().getState()).getZonedDateTime().toLocalDate());
assertEquals(((DateTimeType) item.getState()).getInstant().atZone(ZoneId.systemDefault()).toLocalDate(),
((DateTimeType) result.iterator().next().getState()).getInstant().atZone(ZoneId.systemDefault())
.toLocalDate());
} else {
VerificationHelper.verifyQueryResult(result, item.getState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -176,7 +177,8 @@ private static Pair<Object, Object> handleDecimalType(Object ev, Document doc) {

private static Pair<Object, Object> handleDateTimeType(Object ev, Document doc) {
String value = doc.getString(MongoDBFields.FIELD_VALUE);
return Pair.of(((DateTimeType) ev).getZonedDateTime().toString(), value != null ? value : new Object());
return Pair.of(((DateTimeType) ev).getInstant().atZone(ZoneId.systemDefault()).toString(),
value != null ? value : new Object());
}

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

0 comments on commit 0c11a90

Please sign in to comment.