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:
Oliver Drotbohm
2022-12-31 13:07:43 +01:00
parent 2d933d07c5
commit 652a7b8528
2 changed files with 13 additions and 16 deletions

View File

@@ -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());
}
}

View File

@@ -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));