Skip to content

Commit

Permalink
Fix ClassCastException for LocalDateTime (openhab#13425)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored and leifbladt committed Oct 15, 2022
1 parent 3e33370 commit 439f901
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,14 @@ protected Long objectAsLong(Object v) {
return ((Number) v).longValue();
} else if (v instanceof java.sql.Date) {
return ((java.sql.Date) v).getTime();
} else if (v instanceof LocalDateTime) {
return ((LocalDateTime) v).atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
} else if (v instanceof Instant) {
return ((Instant) v).toEpochMilli();
} else if (v instanceof java.sql.Timestamp) {
return ((java.sql.Timestamp) v).getTime();
}
return ((java.sql.Timestamp) v).getTime();
throw new UnsupportedOperationException("Date of type " + v.getClass().getName() + " is not supported");
}

protected Integer objectAsInteger(Object v) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public void testObjectAsStateReturnsValidState() {
assertInstanceOf(DateTimeType.class, dateTimeType);
assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));

dateTimeType = jdbcBaseDAO.objectAsState(new DateTimeItem("testDateTimeItem"), null,
LocalDateTime.parse("2021-02-01T23:30:02.049"));
assertInstanceOf(DateTimeType.class, dateTimeType);
assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));

State hsbType = jdbcBaseDAO.objectAsState(new ColorItem("testColorItem"), null, "184,100,52");
assertInstanceOf(HSBType.class, hsbType);
assertThat(hsbType, is(HSBType.valueOf("184,100,52")));
Expand Down

0 comments on commit 439f901

Please sign in to comment.