spatie / period

Complex period comparisons
https://spatie.be/open-source
MIT License
1.55k stars 72 forks source link

Hours, minutes and seconds are reset from provided period #107

Closed gerardnll closed 2 years ago

gerardnll commented 2 years ago

Hello,

When creating a Period like this:

Period::make('10:00', '11:00', Precision::MINUTE(), Boundaries::EXCLUDE_END(), 'H:i');

Hours, minutes and seconds are cleared in the PeriodFactory.php lines 107-109:

if (! str_contains($format, ' ')) {
    $dateTime = $dateTime->setTime(0, 0, 0);
}

So this is wrong:

$period1 = Period::make('10:00', '11:00', Precision::MINUTE(), Boundaries::EXCLUDE_END(), 'H:i');
$period2 = Period::make('10:00', '11:00', Precision::MINUTE(), Boundaries::EXCLUDE_END(), 'H:i');

$period1->overlapsWith($period2); // false

Is there a reason why times are being cleared? Could we not do it depending on the precision instead of expecting a date format + time format?

Temporal (or not) fix:

Period::make(Date::now()->setTimeFromTimeString('10:00'), Date::now()->setTimeFromTimeString('11:00'), Precision::MINUTE(), Boundaries::EXCLUDE_END());

freekmurze commented 2 years ago

Feel free to send a PR for this. Make sure to include tests.