From cdfceb1f07b7f55d26d75f8e914467ce405a9990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Tue, 18 Jun 2024 14:53:45 +0200 Subject: [PATCH 1/5] Added some convenient methods for DateTimeType MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../core/library/types/DateTimeType.java | 23 +++++++++++++++++++ .../core/library/types/DateTimeTypeTest.java | 16 +++++++++++++ 2 files changed, 39 insertions(+) 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 132ede449ba..1af484196b5 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 @@ -20,6 +20,7 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; import java.time.zone.ZoneRulesException; import java.util.Locale; @@ -37,6 +38,7 @@ * @author Wouter Born - increase parsing and formatting precision * @author Laurent Garnier - added methods toLocaleZone and toZone * @author Gaël L'hopital - added ability to use second and milliseconds unix time + * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday, sameDay */ @NonNullByDefault public class DateTimeType implements PrimitiveType, State, Command { @@ -248,4 +250,25 @@ private ZonedDateTime parse(String value) throws DateTimeParseException { return date; } + + public boolean isToday() { + return sameDay(ZonedDateTime.now()); + } + + public boolean isTomorrow() { + return sameDay(ZonedDateTime.now().plusDays(1)); + } + + public boolean isYesterday() { + return sameDay(ZonedDateTime.now().minusDays(1)); + } + + public boolean sameDay(DateTimeType other) { + return sameDay(other.zonedDateTime); + } + + public boolean sameDay(ZonedDateTime other) { + return zonedDateTime.truncatedTo(ChronoUnit.DAYS) + .isEqual(other.withZoneSameLocal(zonedDateTime.getZone()).truncatedTo(ChronoUnit.DAYS)); + } } 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 8b240bff5eb..1d7f6d0599c 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 @@ -43,6 +43,7 @@ * @author Erdoan Hadzhiyusein - Added ZonedDateTime tests * @author Laurent Garnier - Enhanced tests * @author Gaël L'hopital - added ability to use second and milliseconds unix time + * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday tests */ @NonNullByDefault public class DateTimeTypeTest { @@ -290,6 +291,21 @@ public void epochTest() { assertThat(epochStandard, is(zdtStandard)); } + @Test + public void relativeTest() { + DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); + DateTimeType dt2 = new DateTimeType("2019-06-12T00:00:00+0000"); + + assertTrue(dt1.sameDay(dt2)); + assertTrue(new DateTimeType().isToday()); + + DateTimeType now = new DateTimeType(); + DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24)); + DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24)); + assertTrue(tomorrow.isTomorrow()); + assertTrue(yesterday.isYesterday()); + } + @ParameterizedTest @MethodSource("parameters") public void createDate(ParameterSet parameterSet) { From 5d4ed154c3672682e6f3d8917ddb1be2dacfc24e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Tue, 18 Jun 2024 14:56:41 +0200 Subject: [PATCH 2/5] Revert "Added some convenient methods for DateTimeType" This reverts commit cdfceb1f07b7f55d26d75f8e914467ce405a9990. --- .../core/library/types/DateTimeType.java | 23 ------------------- .../core/library/types/DateTimeTypeTest.java | 16 ------------- 2 files changed, 39 deletions(-) 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 1af484196b5..132ede449ba 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 @@ -20,7 +20,6 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; -import java.time.temporal.ChronoUnit; import java.time.zone.ZoneRulesException; import java.util.Locale; @@ -38,7 +37,6 @@ * @author Wouter Born - increase parsing and formatting precision * @author Laurent Garnier - added methods toLocaleZone and toZone * @author Gaël L'hopital - added ability to use second and milliseconds unix time - * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday, sameDay */ @NonNullByDefault public class DateTimeType implements PrimitiveType, State, Command { @@ -250,25 +248,4 @@ private ZonedDateTime parse(String value) throws DateTimeParseException { return date; } - - public boolean isToday() { - return sameDay(ZonedDateTime.now()); - } - - public boolean isTomorrow() { - return sameDay(ZonedDateTime.now().plusDays(1)); - } - - public boolean isYesterday() { - return sameDay(ZonedDateTime.now().minusDays(1)); - } - - public boolean sameDay(DateTimeType other) { - return sameDay(other.zonedDateTime); - } - - public boolean sameDay(ZonedDateTime other) { - return zonedDateTime.truncatedTo(ChronoUnit.DAYS) - .isEqual(other.withZoneSameLocal(zonedDateTime.getZone()).truncatedTo(ChronoUnit.DAYS)); - } } 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 1d7f6d0599c..8b240bff5eb 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 @@ -43,7 +43,6 @@ * @author Erdoan Hadzhiyusein - Added ZonedDateTime tests * @author Laurent Garnier - Enhanced tests * @author Gaël L'hopital - added ability to use second and milliseconds unix time - * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday tests */ @NonNullByDefault public class DateTimeTypeTest { @@ -291,21 +290,6 @@ public void epochTest() { assertThat(epochStandard, is(zdtStandard)); } - @Test - public void relativeTest() { - DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); - DateTimeType dt2 = new DateTimeType("2019-06-12T00:00:00+0000"); - - assertTrue(dt1.sameDay(dt2)); - assertTrue(new DateTimeType().isToday()); - - DateTimeType now = new DateTimeType(); - DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24)); - DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24)); - assertTrue(tomorrow.isTomorrow()); - assertTrue(yesterday.isYesterday()); - } - @ParameterizedTest @MethodSource("parameters") public void createDate(ParameterSet parameterSet) { From 8f5bff9f35bf2bf7208266fa0a6a2c5df009812d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Tue, 18 Jun 2024 14:59:00 +0200 Subject: [PATCH 3/5] Adding some relative functions (isToday, isTomorrow, isYesterday and sameDay) to DateTimeType MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../core/library/types/DateTimeType.java | 23 +++++++++++++++++++ .../core/library/types/DateTimeTypeTest.java | 16 +++++++++++++ 2 files changed, 39 insertions(+) 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 132ede449ba..1af484196b5 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 @@ -20,6 +20,7 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; +import java.time.temporal.ChronoUnit; import java.time.zone.ZoneRulesException; import java.util.Locale; @@ -37,6 +38,7 @@ * @author Wouter Born - increase parsing and formatting precision * @author Laurent Garnier - added methods toLocaleZone and toZone * @author Gaël L'hopital - added ability to use second and milliseconds unix time + * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday, sameDay */ @NonNullByDefault public class DateTimeType implements PrimitiveType, State, Command { @@ -248,4 +250,25 @@ private ZonedDateTime parse(String value) throws DateTimeParseException { return date; } + + public boolean isToday() { + return sameDay(ZonedDateTime.now()); + } + + public boolean isTomorrow() { + return sameDay(ZonedDateTime.now().plusDays(1)); + } + + public boolean isYesterday() { + return sameDay(ZonedDateTime.now().minusDays(1)); + } + + public boolean sameDay(DateTimeType other) { + return sameDay(other.zonedDateTime); + } + + public boolean sameDay(ZonedDateTime other) { + return zonedDateTime.truncatedTo(ChronoUnit.DAYS) + .isEqual(other.withZoneSameLocal(zonedDateTime.getZone()).truncatedTo(ChronoUnit.DAYS)); + } } 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 8b240bff5eb..1d7f6d0599c 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 @@ -43,6 +43,7 @@ * @author Erdoan Hadzhiyusein - Added ZonedDateTime tests * @author Laurent Garnier - Enhanced tests * @author Gaël L'hopital - added ability to use second and milliseconds unix time + * @author Gaël L'hopital - added isToday, isTomorrow, isYesterday tests */ @NonNullByDefault public class DateTimeTypeTest { @@ -290,6 +291,21 @@ public void epochTest() { assertThat(epochStandard, is(zdtStandard)); } + @Test + public void relativeTest() { + DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); + DateTimeType dt2 = new DateTimeType("2019-06-12T00:00:00+0000"); + + assertTrue(dt1.sameDay(dt2)); + assertTrue(new DateTimeType().isToday()); + + DateTimeType now = new DateTimeType(); + DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24)); + DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24)); + assertTrue(tomorrow.isTomorrow()); + assertTrue(yesterday.isYesterday()); + } + @ParameterizedTest @MethodSource("parameters") public void createDate(ParameterSet parameterSet) { From a866b07eb484144004ae9b5e43b47567c2cc07dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Wed, 19 Jun 2024 10:36:24 +0200 Subject: [PATCH 4/5] Adding some more functions according Richard's comments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../core/library/types/DateTimeType.java | 63 ++++++++++++++++++- .../core/library/types/DateTimeTypeTest.java | 33 ++++++++-- 2 files changed, 91 insertions(+), 5 deletions(-) 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 1af484196b5..13e3f105f25 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 @@ -269,6 +269,67 @@ public boolean sameDay(DateTimeType other) { public boolean sameDay(ZonedDateTime other) { return zonedDateTime.truncatedTo(ChronoUnit.DAYS) - .isEqual(other.withZoneSameLocal(zonedDateTime.getZone()).truncatedTo(ChronoUnit.DAYS)); + .isEqual(other.withZoneSameInstant(zonedDateTime.getZone()).truncatedTo(ChronoUnit.DAYS)); } + + public DateTimeType toToday() { + return shiftDaysFromToday(0); + } + + public DateTimeType toTomorrow() { + return shiftDaysFromToday(1); + } + + public DateTimeType toYesterday() { + return shiftDaysFromToday(-1); + } + + private DateTimeType shiftDaysFromToday(int days) { + ZonedDateTime now = ZonedDateTime.now().plusDays(days); + return new DateTimeType(zonedDateTime.withYear(now.getYear()).withMonth(now.getMonthValue()) + .withDayOfMonth(now.getDayOfMonth())); + } + + public boolean isBefore(DateTimeType other) { + return zonedDateTime.isBefore(other.zonedDateTime); + } + + public boolean isAfter(DateTimeType other) { + return zonedDateTime.isAfter(other.zonedDateTime); + } + + public boolean isBeforeDate(DateTimeType other) { + return isBeforeDate(other.zonedDateTime); + } + + public boolean isBeforeDate(ZonedDateTime other) { + return zonedDateTime.truncatedTo(ChronoUnit.DAYS).isBefore(other.truncatedTo(ChronoUnit.DAYS)); + } + + public boolean isBeforeTime(DateTimeType other) { + return isBeforeTime(other.zonedDateTime); + } + + public boolean isBeforeTime(ZonedDateTime other) { + return zonedDateTime.withYear(other.getYear()).withMonth(other.getMonthValue()) + .withDayOfMonth(other.getDayOfMonth()).isBefore(other); + } + + public boolean isAfterTime(DateTimeType other) { + return isAfterTime(other.zonedDateTime); + } + + public boolean isAfterTime(ZonedDateTime other) { + return zonedDateTime.withYear(other.getYear()).withMonth(other.getMonthValue()) + .withDayOfMonth(other.getDayOfMonth()).isAfter(other); + } + + public boolean isAfterDate(DateTimeType other) { + return isAfterDate(other.zonedDateTime); + } + + public boolean isAfterDate(ZonedDateTime other) { + return zonedDateTime.truncatedTo(ChronoUnit.DAYS).isAfter(other.truncatedTo(ChronoUnit.DAYS)); + } + } 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 1d7f6d0599c..1f4dab1a64f 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 @@ -293,17 +293,42 @@ public void epochTest() { @Test public void relativeTest() { - DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z"); - DateTimeType dt2 = new DateTimeType("2019-06-12T00:00:00+0000"); + DateTimeType dt1 = new DateTimeType("2019-06-13T01:10:00+02"); + DateTimeType dt2 = new DateTimeType("2019-06-12T23:00:00Z"); + assertTrue(dt1.isAfter(dt2)); + assertTrue(dt2.isBefore(dt1)); assertTrue(dt1.sameDay(dt2)); assertTrue(new DateTimeType().isToday()); DateTimeType now = new DateTimeType(); - DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24)); - DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24)); + DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusDays(1)); + DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusDays(1)); assertTrue(tomorrow.isTomorrow()); assertTrue(yesterday.isYesterday()); + + DateTimeType dt1ToToday = dt1.toToday(); + assertTrue(dt1ToToday.sameDay(now)); + assertEquals(dt1ToToday.getZonedDateTime().getHour(), dt1.getZonedDateTime().getHour()); + assertEquals(dt1ToToday.getZonedDateTime().getMinute(), dt1.getZonedDateTime().getMinute()); + assertEquals(dt1ToToday.getZonedDateTime().getSecond(), dt1.getZonedDateTime().getSecond()); + assertEquals(dt1ToToday.getZonedDateTime().getNano(), dt1.getZonedDateTime().getNano()); + + DateTimeType dt1ToTomorrow = dt1.toTomorrow(); + assertTrue(dt1ToTomorrow.isAfterDate(now)); + + DateTimeType dt1ToYesterday = dt1.toYesterday(); + assertTrue(dt1ToYesterday.isBeforeDate(now)); + + DateTimeType dt3 = new DateTimeType("2019-06-11T23:10:00Z"); + DateTimeType dt4 = new DateTimeType("2019-06-12T23:00:00Z"); + assertFalse(dt3.sameDay(dt4)); + + assertTrue(dt3.isBeforeDate(dt4)); + assertTrue(dt3.isAfterTime(dt4)); + + assertTrue(dt4.isAfter(dt3)); + assertTrue(dt4.isBeforeTime(dt3)); } @ParameterizedTest From 8394e4a24ab638eb3795e68518a1dc7b746860e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20L=27hopital?= Date: Wed, 19 Jun 2024 10:51:26 +0200 Subject: [PATCH 5/5] Correcting spotless MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gaël L'hopital --- .../main/java/org/openhab/core/library/types/DateTimeType.java | 1 - .../java/org/openhab/core/library/types/DateTimeTypeTest.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) 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 13e3f105f25..fe9c54567c3 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 @@ -331,5 +331,4 @@ public boolean isAfterDate(DateTimeType other) { public boolean isAfterDate(ZonedDateTime other) { return zonedDateTime.truncatedTo(ChronoUnit.DAYS).isAfter(other.truncatedTo(ChronoUnit.DAYS)); } - } 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 1f4dab1a64f..d84fe46f559 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 @@ -327,7 +327,7 @@ public void relativeTest() { assertTrue(dt3.isBeforeDate(dt4)); assertTrue(dt3.isAfterTime(dt4)); - assertTrue(dt4.isAfter(dt3)); + assertTrue(dt4.isAfterDate(dt3)); assertTrue(dt4.isBeforeTime(dt3)); }