Skip to content

Commit

Permalink
Fix a logic error wigh working days calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalchevrel committed Sep 8, 2024
1 parent 245d6a9 commit 31f3fa1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
26 changes: 20 additions & 6 deletions app/classes/ReleaseInsights/Duration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Duration

public function __construct(
public readonly Datetime $start,
public readonly Datetime $end)
public readonly Datetime $end
)
{
$this->wellness_days = include DATA . 'wellness_days.php';
}
Expand Down Expand Up @@ -50,9 +51,22 @@ public function weeks(): float
*/
public function isWorkDay(DateTime $day): bool
{
// We substract week-ends and wellness days.
return ! in_array($day->format('l'), ['Saturday','Sunday'])
&& ! in_array($day->format('Y-m-d'), $this->wellness_days);
// We don't consider the current day as a working day
if ($day->format('Y-m-d') === (new DateTime())->format('Y-m-d')) {
return false;
}

// We substract week-end days
if (in_array($day->format('l'), ['Saturday','Sunday'])) {
return false;
}

// We substract wellness days
if (in_array($day->format('Y-m-d'), $this->wellness_days)) {
return false;
}

return true;
}

/**
Expand All @@ -66,13 +80,13 @@ public function workDays(): int
$range = new DatePeriod(start: $this->start, end: $this->end, interval: new DateInterval('P1D'));

foreach($range as $date){

if ($this->isWorkDay($date)) {
$count++;
}
}

// We substract 1 because we don't count the first day of the period
return $count - 1;
return $count;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/Unit/ReleaseInsights/DurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,22 @@

test('Duration->isWorkDay()', function () {
$obj = new Duration(new DateTime('2023-11-01'), new DateTime('2023-12-01'));
expect($obj->isWorkDay(new DateTime('2023-11-01')))
expect($obj->isWorkDay(new DateTime('2023-11-01'))) // Working day
->toBeTrue();
expect($obj->isWorkDay(new DateTime('2023-11-11')))
expect($obj->isWorkDay(new DateTime('2023-11-11'))) // Saturday
->toBeFalse();
expect($obj->isWorkDay(new DateTime('2024-02-16')))
expect($obj->isWorkDay(new DateTime('2023-11-12'))) // Sunday
->toBeFalse();
expect($obj->isWorkDay(new DateTime('2024-02-16'))) // Wellness day
->toBeFalse();
expect($obj->isWorkDay(new DateTime())) // Today
->toBeFalse();
});

test('Duration->workDays()', function () {
$obj = new Duration(new DateTime('2023-11-01'), new DateTime('2023-12-01'));
expect($obj->workDays())
->toBe(21);
->toBe(22);
});

test('Duration->report()', function () {
Expand Down

0 comments on commit 31f3fa1

Please sign in to comment.