From 83d2d03e41e8509abd8d289c9859ea7a27f89175 Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Fri, 5 Mar 2021 17:01:15 +0100 Subject: [PATCH] Fix Duration => string conversion for milliseconds --- src/ExecutionTimer/Duration.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/ExecutionTimer/Duration.php b/src/ExecutionTimer/Duration.php index a60a060..069bbf2 100644 --- a/src/ExecutionTimer/Duration.php +++ b/src/ExecutionTimer/Duration.php @@ -29,10 +29,19 @@ class Duration { */ public function __toString () { return $this->getDays()->asRelativeWhole() . ' ' - . str_pad($this->getHours()->asRelativeWhole(), mb_strlen($this->getHours()->getUnitMax()), '0', STR_PAD_LEFT) . ':' - . str_pad($this->getMinutes()->asRelativeWhole(), mb_strlen($this->getMinutes()->getUnitMax()), '0', STR_PAD_LEFT) . ':' - . str_pad($this->getSeconds()->asRelativeWhole(), mb_strlen($this->getSeconds()->getUnitMax()), '0', STR_PAD_LEFT) . '.' - . str_pad($this->getMilliseconds()->asRelativeWhole(), mb_strlen($this->getMilliseconds()->getUnitMax()), '0', STR_PAD_LEFT); + . self::showUnit($this->getHours()) . ':' + . self::showUnit($this->getMinutes()) . ':' + . self::showUnit($this->getSeconds()) . '.' + . self::showUnit($this->getMilliseconds()); + } + private static function showUnit (DurationUnit $durationUnit) { + $max = $durationUnit->getUnitMax(); + $length = mb_strlen($max); + if (preg_match('/^1[0]*$/', $max)) { + $length -= 1; + } + + return str_pad($durationUnit->asRelativeWhole(), $length, '0', STR_PAD_LEFT); } /** * Convert duration to user friendly string