Skip to content

Commit

Permalink
Add DateUtils::addOpenDays to calculate a date based on open days
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis Fortunier committed Apr 30, 2024
1 parent 098ed9f commit c1fb634
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG for 1.x
===================
## v1.3.1 - (2024-04-30)
### Added
- `DateUtils::addOpenDays` to calculate a date based on open days

## v1.3.0 - (2024-04-29)
### Added
- `ISO8601Formatter` for common date formatting
Expand Down
8 changes: 8 additions & 0 deletions src/Utils/DateUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,14 @@ public static function addDays(\DateTime $dateTime, int $daysNb): \DateTime
return $clonedDateTime;
}

public static function addOpenDays(\DateTime $dateTime, int $daysNb): \DateTime
{
$clonedDateTime = clone $dateTime;
$clonedDateTime->modify("+$daysNb Weekday");

return $clonedDateTime;
}

public static function subDays(\DateTime $dateTime, int $daysNb): \DateTime
{
$clonedDateTime = clone $dateTime;
Expand Down
21 changes: 21 additions & 0 deletions tests/Utils/DateUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,27 @@ public function testAddDays(): void
$this->assertEquals(new \DateTime('2024-10-15 08:00:00'), DateUtils::addDays(new \DateTime('2024-10-10 08:00:00'), 5));
}

/**
* @dataProvider addOpenDaysProvider
*/
public function testAddOpenDays(string $dateTime, int $daysNb, string $expect): void
{
$this->assertSame(
$expect,
DateUtils::addOpenDays(new \DateTime($dateTime), $daysNb)->format('Y-m-d')
);
}

public function addOpenDaysProvider(): array
{
return [
'10_days' => ['2024-03-13', 10, '2024-03-27'],
'20_days' => ['2024-03-01', 20, '2024-03-29'],
'one_day_with_week_end' => ['2024-03-15', 1, '2024-03-18'],
'calculate_on_bissextile_year' => ['2024-02-28', 6, '2024-03-07'],
];
}

public function testSubDays(): void
{
$this->assertEquals(new \DateTime('2024-10-05 08:00:00'), DateUtils::subDays(new \DateTime('2024-10-10 08:00:00'), 5));
Expand Down

0 comments on commit c1fb634

Please sign in to comment.