Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] Add DateTimeType method for returning Instant #3287

Merged
merged 1 commit into from
Dec 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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