Start with monday

This commit is contained in:
Claudio Schaad 2025-09-27 11:04:17 +02:00
parent 32d3fcb7b5
commit 5ce709f4be

View file

@ -23,9 +23,11 @@ public record MonthAndYear(int Month, int Year)
public IReadOnlyList<DateTime> DaysInMonth()
{
return Enumerable.Range(1, DateTime.DaysInMonth(Year, Month))
var dayList = Enumerable.Range(1, DateTime.DaysInMonth(Year, Month))
.Select(d => new DateTime(Year, Month, d))
.ToList();
return EnsureStartsOnMonday(dayList);
}
public IReadOnlyDictionary<int, List<DateTime>> GetWeeksInMonth()
@ -36,4 +38,23 @@ public record MonthAndYear(int Month, int Year)
.ToDictionary(d => d.Key, d => d.ToList());
return weeks;
}
static List<DateTime> EnsureStartsOnMonday(List<DateTime> dates)
{
var firstDate = dates.First();
if (firstDate.DayOfWeek == DayOfWeek.Monday)
return dates;
var result = new List<DateTime>();
var prependDate = firstDate.AddDays(-1);
while (prependDate.DayOfWeek != DayOfWeek.Monday)
{
result.Insert(0, prependDate);
prependDate = prependDate.AddDays(-1);
}
result.Insert(0, prependDate);
result.AddRange(dates);
return result;
}
}