diff --git a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java index b96189dde3a..9a0fd58a036 100644 --- a/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java +++ b/bundles/org.openhab.core/src/main/java/org/eclipse/smarthome/core/library/types/DateTimeType.java @@ -104,10 +104,11 @@ public DateTimeType(String zonedValue) { date = parse("1970-01-01T" + zonedValue); } catch (DateTimeParseException timeOnlyException) { try { - Long epoch = Long.valueOf(zonedValue); + long epoch = Double.valueOf(zonedValue).longValue(); + int length = (int) (Math.log10(epoch >= 0 ? epoch : epoch * -1) + 1); Instant i; // Assume that below 12 digits we're in seconds - if (zonedValue.length() < 12) { + if (length < 12) { i = Instant.ofEpochSecond(epoch); } else { i = Instant.ofEpochMilli(epoch); diff --git a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DecimalTypeTest.java b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DecimalTypeTest.java index 607536dd5af..0e3b75410d3 100644 --- a/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DecimalTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/eclipse/smarthome/core/library/types/DecimalTypeTest.java @@ -121,6 +121,12 @@ public void testConversionToHSBType() { public void testConversionToDateTimeType() { assertEquals(new DateTimeType("2014-03-30T10:58:47+0000"), new DecimalType("1396177127").as(DateTimeType.class)); + assertEquals(new DateTimeType("1969-12-31T23:59:59+0000"), new DecimalType("-1").as(DateTimeType.class)); + assertEquals(new DateTimeType("1970-01-01T00:00:00+0000"), DecimalType.ZERO.as(DateTimeType.class)); + assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1").as(DateTimeType.class)); + assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1.0").as(DateTimeType.class)); + assertEquals(new DateTimeType("1970-01-01T00:00:01+0000"), new DecimalType("1.5").as(DateTimeType.class)); + assertEquals(new DateTimeType("1969-12-31T23:59:59+0000"), new DecimalType("-1.0").as(DateTimeType.class)); } @Test