From 100aa6e8388d977478f67ff0d565a14bfbf75f59 Mon Sep 17 00:00:00 2001 From: Jacob Laursen Date: Fri, 30 Dec 2022 20:40:58 +0100 Subject: [PATCH] Add method to DateTimeType for returning Instant (#3287) Signed-off-by: Jacob Laursen --- .../core/library/types/DateTimeType.java | 9 +++++++++ .../core/library/types/DateTimeTypeTest.java | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java index 0a6f85585e6..cb2cbe348a6 100644 --- a/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java +++ b/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/DateTimeType.java @@ -119,6 +119,15 @@ public ZonedDateTime getZonedDateTime() { return zonedDateTime; } + /** + * Get curent object represented as an {@link Instant} + * + * @return an {@link Instant} representation of the current object + */ + public Instant getInstant() { + return zonedDateTime.toInstant(); + } + public static DateTimeType valueOf(String value) { return new DateTimeType(value); } diff --git a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java index ceb2e26057f..342bfb1a0d1 100644 --- a/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java +++ b/bundles/org.openhab.core/src/test/java/org/openhab/core/library/types/DateTimeTypeTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.*; import java.time.DateTimeException; +import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; @@ -242,11 +243,12 @@ public void equalityTest() { assertTrue(dt1.toString().equals(dt2.toFullString())); assertTrue(dt1.getZonedDateTime().equals(dt2.getZonedDateTime())); + assertTrue(dt1.getInstant().equals(dt2.getInstant())); assertTrue(dt1.equals(dt2)); } @Test - public void parsingTest() { + public void zonedParsingTest() { DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); DateTimeType dt2 = new DateTimeType("2019-06-12T17:30:00+0000"); DateTimeType dt3 = new DateTimeType("2019-06-12T19:30:00+0200"); @@ -261,6 +263,20 @@ public void parsingTest() { assertThat(zdt2, is(zdt3.withZoneSameInstant(zdt2.getZone()))); } + @Test + public void instantParsingTest() { + DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); + DateTimeType dt2 = new DateTimeType("2019-06-12T17:30:00+0000"); + DateTimeType dt3 = new DateTimeType("2019-06-12T19:30:00+0200"); + assertThat(dt1, is(dt2)); + + Instant i1 = dt1.getInstant(); + Instant i2 = dt2.getInstant(); + Instant i3 = dt3.getInstant(); + assertThat(i1, is(i2)); + assertThat(i1, is(i3)); + } + @Test public void epochTest() { DateTimeType zdtEpoch = new DateTimeType("1970-01-01T00:00:00+0000");