Skip to content

Commit

Permalink
Merge pull request #43 from hughgrigg/carbon-instance-signature
Browse files Browse the repository at this point in the history
Interval instance signature: match Carbon
  • Loading branch information
hughgrigg authored Nov 17, 2018
2 parents 1e3bff0 + a671786 commit 83bb5e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ reports/
build/
coverage/
.idea/

composer.lock
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hughgrigg/php-business-time",
"description": "Business time / working days extension for Carbon dates",
"version": "1.1.1",
"version": "1.1.2",
"type": "library",
"require": {
"php": "^7.0",
Expand Down
29 changes: 22 additions & 7 deletions src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,36 @@ public function forHumans($short = false): string
* Normalise instance creation to seconds.
*
* @param DateInterval $dateInterval
* @param bool $trimMicroseconds
*
* @return static
*/
public static function instance(DateInterval $dateInterval): self
{
return self::seconds(self::intervalToSeconds($dateInterval));
public static function instance(
DateInterval $dateInterval,
$trimMicroseconds = true
): self {
return self::seconds(
self::intervalToSeconds($dateInterval, $trimMicroseconds)
);
}

/**
* @param DateInterval $interval
* @param DateInterval $dateInterval
* @param bool $trimMicroseconds
*
* @return int
*/
public static function intervalToSeconds(DateInterval $interval): int
{
return (new Carbon())->add($interval)->diffInRealSeconds(new Carbon());
public static function intervalToSeconds(
DateInterval $dateInterval,
$trimMicroseconds = true
): int {
$dateInterval->f =
$trimMicroseconds ||
version_compare(PHP_VERSION, '7.1.0-dev', '<') ? 0 :
$dateInterval->f;

return (new Carbon())->add($dateInterval)->diffInRealSeconds(
new Carbon()
);
}
}

0 comments on commit 83bb5e0

Please sign in to comment.