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); sleep(3);
$timer->step('sleep 1'); $timer->step('sleep 1');
echo $timer->getLastStepRelativeDuration()->toUserString() . PHP_EOL; echo 'sleep 1' . PHP_EOL;
echo $timer->getLastStepDuration() . PHP_EOL; echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
sleep(2); sleep(2);
$timer->step('sleep 2'); $timer->step('sleep 2');
echo $timer->getLastStepRelativeDuration()->toUserString() . PHP_EOL; echo 'sleep 2' . PHP_EOL;
echo $timer->getLastStepDuration() . PHP_EOL; echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
$timer->stop(); echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
sleep(3);
$final = $timer->getDuration(); $timer->step('sleep 3');
echo $timer->getRelativeDuration()->toUserString() . PHP_EOL; echo 'sleep 3' . PHP_EOL;
echo $timer->getRelativeDuration() . PHP_EOL; echo "\t" . $timer->getLastStepRelativeDuration() . PHP_EOL;
echo "\t" . $timer->getLastStepDuration() . PHP_EOL;
echo $timer->getDuration()->toUserString() . PHP_EOL; $timer->stop();
echo $timer->getDuration() . PHP_EOL; 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 () { public function __toString () {
return $this->getDays()->asRelativeWhole() . ' ' return $this->getDays()->asRelativeWhole() . ' '
. $this->getHours()->asRelativeWhole() . ':' . str_pad($this->getHours()->asRelativeWhole(), mb_strlen($this->getHours()->getUnitMax()), '0', STR_PAD_LEFT) . ':'
. $this->getMinutes()->asRelativeWhole() . ':' . str_pad($this->getMinutes()->asRelativeWhole(), mb_strlen($this->getMinutes()->getUnitMax()), '0', STR_PAD_LEFT) . ':'
. $this->getSeconds()->asRelativeWhole() . '.' . str_pad($this->getSeconds()->asRelativeWhole(), mb_strlen($this->getSeconds()->getUnitMax()), '0', STR_PAD_LEFT) . '.'
. $this->getMilliseconds()->asRelativeWhole() . str_pad($this->getMilliseconds()->asRelativeWhole(), mb_strlen($this->getMilliseconds()->getUnitMax()), '0', STR_PAD_LEFT);
. $this->getMicroseconds()->asRelativeWhole();
} }
/** /**
* Convert duration to user friendly string * Convert duration to user friendly string

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

Loading…
Cancel
Save