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

Relative functions for DateTimeType #4276

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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 @@ -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;

Expand All @@ -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 {
Expand Down Expand Up @@ -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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use witZoneSameInstant ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Copy link
Contributor

@jimtng jimtng Jun 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To ensure that this checks for the same instant:

dt1 = "2019-06-13T01:10:00+02"
dt2 = "2019-06-12T23:00:00Z"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed


assertTrue(dt1.sameDay(dt2));
assertTrue(new DateTimeType().isToday());

DateTimeType now = new DateTimeType();
DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusHours(24));
DateTimeType tomorrow = new DateTimeType(now.getZonedDateTime().plusDays(1));

I know this is an edge case, but the length of the day isn't always precisely 24h.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to avoid using the same method than what's used in the object itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusHours(24));
DateTimeType yesterday = new DateTimeType(now.getZonedDateTime().minusdays(1));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

assertTrue(tomorrow.isTomorrow());
assertTrue(yesterday.isYesterday());
}

@ParameterizedTest
@MethodSource("parameters")
public void createDate(ParameterSet parameterSet) {
Expand Down