-
-
Notifications
You must be signed in to change notification settings - Fork 429
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
base: main
Are you sure you want to change the base?
Changes from all commits
cdfceb1
5d4ed15
8f5bff9
a866b07
8394e4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,85 @@ 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.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())); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also has the same ambiguity issue related to differences in time zones between what's in
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Made it relative to |
||
public boolean isBefore(DateTimeType other) { | ||
return zonedDateTime.isBefore(other.zonedDateTime); | ||
} | ||
|
||
public boolean isAfter(DateTimeType other) { | ||
return zonedDateTime.isAfter(other.zonedDateTime); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might also need the versions of isBefore and isAfter that accept a ZonedDateTime There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These already exists, reason why I thought it wasn't needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically it's for convenience, so you can do DateTimeItem.state.isBefore(zdt) instead of DateTimeItem.state.getZonedDateTime().isBefore(zdt), and also for uniformity since you are adding the ability to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added |
||
public boolean isBeforeDate(DateTimeType other) { | ||
return isBeforeDate(other.zonedDateTime); | ||
} | ||
|
||
public boolean isBeforeDate(ZonedDateTime other) { | ||
return zonedDateTime.truncatedTo(ChronoUnit.DAYS).isBefore(other.truncatedTo(ChronoUnit.DAYS)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am against the introduction of isBeforeDate, isBeforeTime, etc. because their meaning is ambiguous depending on the time zones of the two objects involved. Take two zdt objects containing the same date but within two different time zones. They would fail the given comparison.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So do you recommend I should align Timezones before comparison, rename the methods or drop them ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to drop them, but that's just an opinion. Again, I defer to the maintainers to decide. |
||
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)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isEqual already compares the instant so it's not necessary to equalise the timezone prior to comparison.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the truncation to ChronoUnit.DAYS, the comparison is false if they are not moved in the same time zone.