Better conversion Duration => string

master 1.0.1
Julien Rosset 4 years ago
parent 2763cbdb87
commit 517d3c63db

@ -10,19 +10,25 @@ $timer->start();
sleep(3);
$timer->step('sleep 1');
echo $timer->getLastStepRelativeDuration()->toUserString() . PHP_EOL;
echo $timer->getLastStepDuration() . PHP_EOL;
echo 'sleep 1' . PHP_EOL;
echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
sleep(2);
$timer->step('sleep 2');
echo $timer->getLastStepRelativeDuration()->toUserString() . PHP_EOL;
echo $timer->getLastStepDuration() . PHP_EOL;
$timer->stop();
echo 'sleep 2' . PHP_EOL;
echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
sleep(3);
$final = $timer->getDuration();
echo $timer->getRelativeDuration()->toUserString() . PHP_EOL;
echo $timer->getRelativeDuration() . PHP_EOL;
$timer->step('sleep 3');
echo 'sleep 3' . PHP_EOL;
echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
echo $timer->getDuration()->toUserString() . PHP_EOL;
echo $timer->getDuration() . PHP_EOL;
$timer->stop();
echo 'STOP' . PHP_EOL;
echo "\t" . $timer->getRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getDuration() . PHP_EOL;

@ -29,11 +29,10 @@ class Duration {
*/
public function __toString () {
return $this->getDays()->asRelativeWhole() . ' '
. $this->getHours()->asRelativeWhole() . ':'
. $this->getMinutes()->asRelativeWhole() . ':'
. $this->getSeconds()->asRelativeWhole() . '.'
. $this->getMilliseconds()->asRelativeWhole()
. $this->getMicroseconds()->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);
}
/**
* Convert duration to user friendly string

@ -47,6 +47,14 @@ class DurationUnit {
$this->unit_max = $units_max[$unit];
}
/**
* The time unit
*
* @return int The maximum value of time unit
*/
public function getUnit () {
return $this->unit;
}
/**
* The maximum value of time unit
*
@ -55,8 +63,8 @@ class DurationUnit {
*
* @return int The maximum value of time unit
*/
public function getUnit () {
return $this->unit;
public function getUnitMax () {
return $this->unit_max;
}
/**

Loading…
Cancel
Save