Skip to content

Commit

Permalink
Add Date::offsetByMonths and Date::offsetByYears methods (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-j authored Jun 10, 2024
1 parent 154c939 commit 88a757c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
18 changes: 18 additions & 0 deletions lib/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
66 changes: 64 additions & 2 deletions tests/unit/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ public function it_calculates_diff_correctly(): void
/**
* @return iterable<array{int}>
*/
public static function provideNumberOfDayOffsets(): iterable
public static function provideNumberOfDaysOffsets(): iterable
{
yield [-10];
yield [-5];
Expand All @@ -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
{
Expand All @@ -424,6 +424,68 @@ public function it_can_be_offset_by_days(int $days): void
$this->assertTrue($expectedDate->equals($offsetDate));
}

/**
* @return iterable<array{int}>
*/
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<array{int}>
*/
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
{
Expand Down

0 comments on commit 88a757c

Please sign in to comment.