Skip to content

Commit e4a32f7

Browse files
committed
fix limit days regression, rewrite, #3840
1 parent 76da0aa commit e4a32f7

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Thanks to: @dathbe.
3737
- [calendar] Fixed broken unittest that only broke on the 1st of July and 1st of january (#3830)
3838
- [clock] Fixed missing icons when no other modules with icons is loaded (#3834)
3939
- [weather] Fixed handling of empty values in weathergov providers handling of precipitationAmount (#3859)
40+
- [calendar] Fix regression handling of limit days (#3840)
4041

4142
## [2.32.0] - 2025-07-01
4243

modules/default/calendar/calendar.js

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -705,30 +705,24 @@ Module.register("calendar", {
705705
* Limit the number of days displayed
706706
* If limitDays is set > 0, limit display to that number of days
707707
*/
708-
if (this.config.limitDays > 0) {
709-
let newEvents = [];
710-
let lastDate = today.clone().subtract(1, "days");
711-
let days = 0;
712-
for (const ev of events) {
713-
let eventDate = this.timestampToMoment(ev.startDate);
714-
715-
/*
716-
* if date of event is later than lastdate
717-
* check if we already are showing max unique days
718-
*/
719-
if (eventDate.isAfter(lastDate)) {
720-
// if the only entry in the first day is a full day event that day is not counted as unique
721-
if (!this.config.limitDaysNeverSkip && newEvents.length === 1 && days === 1 && newEvents[0].fullDayEvent) {
722-
days--;
723-
}
724-
days++;
725-
if (days > this.config.limitDays) {
726-
continue;
727-
} else {
728-
lastDate = eventDate;
729-
}
708+
if (this.config.limitDays > 0 && events.length > 0) {
709+
// Group all events by date, events on the same date will be in a list with the key being the date.
710+
const eventsByDate = Object.groupBy(events, (ev) => this.timestampToMoment(ev.startDate).format("YYYY-MM-DD"));
711+
const newEvents = [];
712+
let currentDate = moment();
713+
let daysCollected = 0;
714+
715+
while (daysCollected < this.config.limitDays) {
716+
const dateStr = currentDate.format("YYYY-MM-DD");
717+
// Check if there are events on the currentDate
718+
if (eventsByDate[dateStr] && eventsByDate[dateStr].length > 0) {
719+
// If there are any events today then get all those events and select the currently active events and the events that are starting later in the day.
720+
newEvents.push(...eventsByDate[dateStr].filter((ev) => this.timestampToMoment(ev.endDate).isAfter(moment())));
721+
// Since we found a day with events, increase the daysCollected by 1
722+
daysCollected++;
730723
}
731-
newEvents.push(ev);
724+
// Search for the next day
725+
currentDate.add(1, "day");
732726
}
733727
events = newEvents;
734728
}

0 commit comments

Comments
 (0)