From 88a757cd0863d45ba81fc394d5f3ebb88ff13dd3 Mon Sep 17 00:00:00 2001 From: John Jablonski Date: Mon, 10 Jun 2024 10:45:16 +0200 Subject: [PATCH] Add Date::offsetByMonths and Date::offsetByYears methods (#694) --- lib/Date.php | 18 +++++++++++ tests/unit/DateTest.php | 66 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/lib/Date.php b/lib/Date.php index ea1b0f41..add6bdb0 100644 --- a/lib/Date.php +++ b/lib/Date.php @@ -103,6 +103,24 @@ public function offsetByDays(int $days): self return $this->modify(\sprintf('%d days', $days)); } + public function offsetByMonths(int $months): self + { + if ($months === 0) { + return $this; + } + + return $this->modify(\sprintf('%d months', $months)); + } + + public function offsetByYears(int $years): self + { + if ($years === 0) { + return $this; + } + + return $this->modify(\sprintf('%d years', $years)); + } + /** * Provides a date time representation of this date in UTC. */ diff --git a/tests/unit/DateTest.php b/tests/unit/DateTest.php index b04a4fb1..7316e044 100644 --- a/tests/unit/DateTest.php +++ b/tests/unit/DateTest.php @@ -396,7 +396,7 @@ public function it_calculates_diff_correctly(): void /** * @return iterable */ - public static function provideNumberOfDayOffsets(): iterable + public static function provideNumberOfDaysOffsets(): iterable { yield [-10]; yield [-5]; @@ -407,7 +407,7 @@ public static function provideNumberOfDayOffsets(): iterable yield [10]; } - #[DataProvider('provideNumberOfDayOffsets')] + #[DataProvider('provideNumberOfDaysOffsets')] #[Test] public function it_can_be_offset_by_days(int $days): void { @@ -424,6 +424,68 @@ public function it_can_be_offset_by_days(int $days): void $this->assertTrue($expectedDate->equals($offsetDate)); } + /** + * @return iterable + */ + public static function provideNumberOfMonthsOffsets(): iterable + { + yield [-10]; + yield [-5]; + yield [-1]; + yield [0]; + yield [1]; + yield [5]; + yield [10]; + } + + #[DataProvider('provideNumberOfMonthsOffsets')] + #[Test] + public function it_can_be_offset_by_months(int $days): void + { + $date = Date::fromYearMonthDay(2022, 5, 1); + + $offsetDate = $date->offsetByMonths($days); + + $expectedDate = Date::fromDateTime($date->toDateTime()->modify(\sprintf('%d months', $days))); + + if ($days !== 0) { + $this->assertNotSame($date, $offsetDate); + } + + $this->assertTrue($expectedDate->equals($offsetDate)); + } + + /** + * @return iterable + */ + public static function provideNumberOfYearsOffsets(): iterable + { + yield [-10]; + yield [-5]; + yield [-1]; + yield [0]; + yield [1]; + yield [5]; + yield [10]; + } + + #[DataProvider('provideNumberOfYearsOffsets')] + #[Test] + public function it_can_be_offset_by_years(int $days): void + { + $date = Date::fromYearMonthDay(2022, 5, 1); + + $offsetDate = $date->offsetByYears($days); + + $expectedDate = Date::fromDateTime($date->toDateTime()->modify(\sprintf('%d years', $days))); + + if ($days !== 0) { + $this->assertNotSame($date, $offsetDate); + } + + $this->assertTrue($expectedDate->equals($offsetDate)); + } + #[Test] public function it_can_be_constructed_from_a_number_of_years_ago(): void {