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