Fix broken month view if last week of December is in next year (#9)

The cause was using the wrong character in formatting the year. We now
switch from `Y` to `o` which will work based on the week instead of
date. This is necessary as we provide this year to the week, and
therefore need the year of the week, not day.

Resolves: #11388
This commit is contained in:
Daniel Siepmann (Codappix) 2024-09-16 12:29:46 +02:00 committed by GitHub
parent 33eb0a2a3b
commit b087098793
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 31 additions and 4 deletions

View file

@ -78,7 +78,7 @@ class Month
while ($currentDay <= $lastDay) { while ($currentDay <= $lastDay) {
$this->weeks[] = new Week( $this->weeks[] = new Week(
(int) $currentDay->format('W'), (int) $currentDay->format('W'),
(int) $currentDay->format('Y') (int) $currentDay->format('o')
); );
$currentDay = $currentDay->modify('+7 days'); $currentDay = $currentDay->modify('+7 days');

View file

@ -80,7 +80,7 @@ class Week
return new self( return new self(
(int) $newDay->format('W'), (int) $newDay->format('W'),
(int) $newDay->format('Y') (int) $newDay->format('o')
); );
} }
@ -90,7 +90,7 @@ class Week
return new self( return new self(
(int) $newDay->format('W'), (int) $newDay->format('W'),
(int) $newDay->format('Y') (int) $newDay->format('o')
); );
} }

View file

@ -14,7 +14,12 @@ Nothing
Fixes Fixes
----- -----
Nothing * Fix broken month view if last week of December is in next year.
The cause was using the wrong character in formatting the year.
We now switch from `Y` to `o` which will work based on the week instead of date.
This is necessary as we provide this year to the week,
and therefore need the year of the week, not day.
Tasks Tasks
----- -----

View file

@ -138,6 +138,28 @@ class MonthTest extends TestCase
self::assertSame('2021-02-28', $result[27]->getDateTimeInstance()->format('Y-m-d')); self::assertSame('2021-02-28', $result[27]->getDateTimeInstance()->format('Y-m-d'));
} }
/**
* @test
*/
public function returnsWeeksIfLastDecemberWeekIsInNextYear(): void
{
$subject = new Month(12, 2024);
$result = $subject->getWeeks();
self::assertCount(6, $result);
$week = array_pop($result);
$days = $week->getDays();
self::assertSame('2024-12-30', $days[0]->getDateTimeInstance()->format('Y-m-d'));
self::assertSame('2025-01-05', $days[6]->getDateTimeInstance()->format('Y-m-d'));
$week = array_pop($result);
$days = $week->getDays();
self::assertSame('2024-12-23', $days[0]->getDateTimeInstance()->format('Y-m-d'));
self::assertSame('2024-12-29', $days[6]->getDateTimeInstance()->format('Y-m-d'));
}
/** /**
* @test * @test
*/ */