Skip to content

Commit

Permalink
Add method to DateTimeType for returning Instant (openhab#3287)
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
GitOrigin-RevId: 100aa6e
  • Loading branch information
jlaur authored and splatch committed Jul 12, 2023
1 parent 6a95f8b commit fa6d944
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand Down

0 comments on commit fa6d944

Please sign in to comment.