GH-97 - Rework calculation of coordinates for WeekHasPassed events.
They now also work correctly when shifting time across a year's end.
This commit is contained in:
@@ -82,12 +82,19 @@ public class Moments {
|
||||
// Day has passed
|
||||
events.publishEvent(DayHasPassed.of(date));
|
||||
|
||||
// Week has passed
|
||||
int week = getWeekOfYear(date);
|
||||
Year year = Year.from(date);
|
||||
var year = Year.from(date);
|
||||
|
||||
if (getWeekOfYear(date.plusDays(1)) > week) {
|
||||
events.publishEvent(WeekHasPassed.of(year, week, properties.getLocale()));
|
||||
// Week has passed
|
||||
var weekFields = WeekFields.of(properties.getLocale());
|
||||
var field = weekFields.weekOfWeekBasedYear();
|
||||
var currentWeek = date.get(field);
|
||||
var tomorrowsWeek = date.plusDays(1).get(field);
|
||||
|
||||
if (tomorrowsWeek != currentWeek) {
|
||||
|
||||
var eventYear = Year.of(date.get(weekFields.weekBasedYear()));
|
||||
|
||||
events.publishEvent(WeekHasPassed.of(eventYear, currentWeek, properties.getLocale()));
|
||||
}
|
||||
|
||||
// Month has passed
|
||||
@@ -146,14 +153,4 @@ public class Moments {
|
||||
|
||||
return LocalDateTime.ofInstant(instant, properties.getZoneId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the week of the year for the given {@link LocalDate}.
|
||||
*
|
||||
* @param date must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private int getWeekOfYear(LocalDate date) {
|
||||
return date.get(WeekFields.of(properties.getLocale()).weekOfYear());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ class MomentsUnitTests {
|
||||
MomentsProperties properties = MomentsProperties.DEFAULTS.withLocale(locale);
|
||||
|
||||
LocalDate now = LocalDate.now(clock);
|
||||
int weekOfYear = now.get(WeekFields.of(locale).weekOfYear());
|
||||
int weekOfYear = now.get(WeekFields.of(locale).weekOfWeekBasedYear());
|
||||
|
||||
new Moments(clock, events, properties).shiftBy(Duration.ofDays(7));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user