-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 1862684: Add relative weeks/days for train dates
- Add Duration class to calculate date intervals and working days (exclude week-ends) - We ignore hours for all interval calculations - Add tooltips on hover on dates of future releases view to indicate working days left - Show text with the mention of working days left only for expanded items - Clicking on the date now opens toggles the details tag of the row to display the working days
- Loading branch information
1 parent
9c6ee63
commit 4e25de7
Showing
9 changed files
with
285 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ReleaseInsights; | ||
|
||
use DateInterval, DatePeriod, DateTime; | ||
|
||
/** | ||
* Calculate durations in days and working days to indicate working days left in schedules | ||
*/ | ||
class Duration | ||
{ | ||
public function __construct(public readonly Datetime $start, public readonly Datetime $end) | ||
{ | ||
} | ||
|
||
/** | ||
* Get The number of days between 2 dates | ||
*/ | ||
public function days(): int | ||
{ | ||
return $this->start->diff($this->end)->days; | ||
} | ||
|
||
/** | ||
* Get The number of weeks between 2 dates | ||
* We round down to 0.5 | ||
*/ | ||
public function weeks(): float | ||
{ | ||
// We are limiting to 1 decimal for the week | ||
$weeks = number_format($this->days()/7, 1); | ||
|
||
// We want some logic to round down only the decimal to 0.5 | ||
[$main, $minor] = explode('.', (string) $weeks); | ||
$minor = $minor >= 5 ? 5 : 0; | ||
|
||
return (float) ($main . '.' . $minor); | ||
} | ||
|
||
/** | ||
* Check if a day is a workday | ||
*/ | ||
public function isWorkDay(DateTime $day): bool | ||
{ | ||
/** | ||
* We only substract week-ends, we may add more logic to | ||
* also remove wellness days in the future. | ||
*/ | ||
return ! in_array($day->format('l'), ['Saturday','Sunday']); | ||
} | ||
|
||
/** | ||
* Get The number of working days between 2 dates | ||
*/ | ||
public function workDays(): int | ||
{ | ||
$count = 0; | ||
|
||
// P1D is short for 'Period: 1 Day' | ||
$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 all the data in an array for template use | ||
* | ||
* @return array<string, float|int> | ||
*/ | ||
public function report(): array | ||
{ | ||
return [ | ||
'days' => $this->days(), | ||
'workdays' => $this->workDays(), | ||
'weeks' => $this->weeks(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.