From b9105bde38a1fe5cb077715de0aa9bd495ea7036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Mon, 25 Nov 2019 23:21:44 +0100 Subject: [PATCH] Correcting Issue #1231 (#1240) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Forgot to test decimal negatives Signed-off-by: Gaƫl L'hopital --- .../eclipse/smarthome/core/library/types/DateTimeType.java | 5 +++-- .../smarthome/core/library/types/DecimalTypeTest.java | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) 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