-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from spatie/feature/issue-139-diff-methods
Fix #139 Add diff methods
- Loading branch information
Showing
5 changed files
with
174 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.idea | ||
build | ||
composer.lock | ||
coverage | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace Spatie\OpeningHours\Helpers; | ||
|
||
use DateTimeInterface; | ||
|
||
trait DiffTrait | ||
{ | ||
private function diffInSeconds(string $stateCheckMethod, string $nextDateMethod, string $skipDateMethod, DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
$time = 0; | ||
|
||
if ($endDate < $startDate) { | ||
return -$this->diffInSeconds($stateCheckMethod, $nextDateMethod, $skipDateMethod, $endDate, $startDate); | ||
} | ||
|
||
$date = $startDate; | ||
|
||
while ($date < $endDate) { | ||
if ($this->$stateCheckMethod($date)) { | ||
$date = $this->$skipDateMethod($date); | ||
continue; | ||
} | ||
|
||
$nextDate = min($endDate, $this->$nextDateMethod($date)); | ||
$time += floatval($nextDate->format('U.u')) - floatval($date->format('U.u')); | ||
$date = $nextDate; | ||
} | ||
|
||
return $time; | ||
} | ||
|
||
/** | ||
* Return the amount of open time (number of seconds as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInOpenSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInSeconds('isClosedAt', 'nextClose', 'nextOpen', $startDate, $endDate); | ||
} | ||
|
||
/** | ||
* Return the amount of open time (number of minutes as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInOpenMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInOpenSeconds($startDate, $endDate) / 60; | ||
} | ||
|
||
/** | ||
* Return the amount of open time (number of hours as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInOpenHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInOpenMinutes($startDate, $endDate) / 60; | ||
} | ||
|
||
/** | ||
* Return the amount of closed time (number of seconds as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInClosedSeconds(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInSeconds('isOpenAt', 'nextOpen', 'nextClose', $startDate, $endDate); | ||
} | ||
|
||
/** | ||
* Return the amount of closed time (number of minutes as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInClosedMinutes(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInClosedSeconds($startDate, $endDate) / 60; | ||
} | ||
|
||
/** | ||
* Return the amount of closed time (number of hours as a floating number) between 2 dates/times. | ||
* | ||
* @param DateTimeInterface $startDate | ||
* @param DateTimeInterface $endDate | ||
* | ||
* @return float | ||
*/ | ||
public function diffInClosedHours(DateTimeInterface $startDate, DateTimeInterface $endDate): float | ||
{ | ||
return $this->diffInClosedMinutes($startDate, $endDate) / 60; | ||
} | ||
} |
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