Skip to content

Commit

Permalink
Internval instance signature: match Carbon
Browse files Browse the repository at this point in the history
  • Loading branch information
hughgrigg committed Nov 17, 2018
1 parent 1e3bff0 commit 80ae979
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 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
31 changes: 24 additions & 7 deletions src/Interval.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,38 @@ 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 {
$microseconds =
$trimMicroseconds ||
version_compare(PHP_VERSION, '7.1.0-dev', '<') ? 0 :
$dateInterval->f;
if ($microseconds) {
$dateInterval->f = $microseconds;
}
return (new Carbon())->add($dateInterval)->diffInRealSeconds(
new Carbon()
);
}
}

0 comments on commit 80ae979

Please sign in to comment.