diff --git a/src/LocalDate.php b/src/LocalDate.php index 6387b44a..cc5336b3 100644 --- a/src/LocalDate.php +++ b/src/LocalDate.php @@ -455,8 +455,8 @@ public function withDay(int $day): LocalDate public function plusPeriod(Period $period): LocalDate { return $this - ->plusYears($period->getYears()) - ->plusMonths($period->getMonths()) + // Not plusYears()->plusMonths(), which would clamp Feb 29 twice: 2020-02-29 + P1Y1M would give 2021-03-28. + ->plusMonths($period->getYears() * 12 + $period->getMonths()) ->plusDays($period->getDays()); } diff --git a/tests/LocalDateTest.php b/tests/LocalDateTest.php index 8f134910..a6a7f79d 100644 --- a/tests/LocalDateTest.php +++ b/tests/LocalDateTest.php @@ -984,6 +984,12 @@ public static function providerPeriod(): array [2001, 2, 3, 0, 30, 50, 2003, 9, 22], [2001, 2, 3, 0, 0, -30, 2001, 1, 4], [2001, 2, 3, 0, -30, -50, 1998, 6, 14], + [2020, 2, 29, 1, 0, 0, 2021, 2, 28], + [2020, 2, 29, 1, 1, 0, 2021, 3, 29], + [2020, 2, 29, 1, -1, 0, 2021, 1, 29], + [2020, 2, 29, 1, 6, 0, 2021, 8, 29], + [2020, 2, 29, -1, -1, 0, 2019, 1, 29], + [999995, 2, 26, 5, -29, 5, 999997, 10, 1], ]; } diff --git a/tests/LocalDateTimeTest.php b/tests/LocalDateTimeTest.php index 8eedf007..0caf2812 100644 --- a/tests/LocalDateTimeTest.php +++ b/tests/LocalDateTimeTest.php @@ -652,6 +652,12 @@ public static function providerPeriod(): array [2001, 2, 3, 0, 30, 50, 2003, 9, 22], [2001, 2, 3, 0, 0, -30, 2001, 1, 4], [2001, 2, 3, 0, -30, -50, 1998, 6, 14], + [2020, 2, 29, 1, 0, 0, 2021, 2, 28], + [2020, 2, 29, 1, 1, 0, 2021, 3, 29], + [2020, 2, 29, 1, -1, 0, 2021, 1, 29], + [2020, 2, 29, 1, 6, 0, 2021, 8, 29], + [2020, 2, 29, -1, -1, 0, 2019, 1, 29], + [999995, 2, 26, 5, -29, 5, 999997, 10, 1], ]; }