mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Make DateTimeType Comparable (#4364)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
+7
-1
@@ -37,9 +37,10 @@ import org.openhab.core.types.State;
|
||||
* @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 Jimmy Tanagra - implement Comparable
|
||||
*/
|
||||
@NonNullByDefault
|
||||
public class DateTimeType implements PrimitiveType, State, Command {
|
||||
public class DateTimeType implements PrimitiveType, State, Command, Comparable<DateTimeType> {
|
||||
|
||||
// external format patterns for output
|
||||
public static final String DATE_PATTERN = "yyyy-MM-dd'T'HH:mm:ss";
|
||||
@@ -229,6 +230,11 @@ public class DateTimeType implements PrimitiveType, State, Command {
|
||||
return zonedDateTime.compareTo(other.zonedDateTime) == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(DateTimeType o) {
|
||||
return zonedDateTime.compareTo(o.getZonedDateTime());
|
||||
}
|
||||
|
||||
private ZonedDateTime parse(String value) throws DateTimeParseException {
|
||||
ZonedDateTime date;
|
||||
try {
|
||||
|
||||
+12
@@ -248,6 +248,18 @@ public class DateTimeTypeTest {
|
||||
assertTrue(dt1.equals(dt2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void comparabilityTest() {
|
||||
ZonedDateTime zoned = ZonedDateTime.now();
|
||||
DateTimeType dt1 = new DateTimeType(zoned);
|
||||
DateTimeType dt2 = new DateTimeType(zoned.plusSeconds(1));
|
||||
DateTimeType dt3 = new DateTimeType(zoned.minusSeconds(1));
|
||||
|
||||
assertTrue(dt1.compareTo(dt2) < 0);
|
||||
assertTrue(dt1.compareTo(dt3) > 0);
|
||||
assertTrue(dt1.compareTo(dt1) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void zonedParsingTest() {
|
||||
DateTimeType dt1 = new DateTimeType("2019-06-12T17:30:00Z");
|
||||
|
||||
Reference in New Issue
Block a user