@ 2019-11-11T11:36:50Z <?php
namespace Core\component\time;
final class TimeFactory {
/** @var int */
private $year;
/** @var int */
private $weekNumber;
/** @var int */
private $day;
/** @var int */
private $hour;
/** @var int */
private $minute;
/**
* @param string $formatted
* @return static
*/
public static function createFromString(string $formatted): self {
if (!preg_match('/^(\d{4}):(\d{2}):(\d{1}):(\d{2}):(\d{2})$/', $formatted, $matches)) {
throw new \InvalidArgumentException('Invalid currency format.');
}
return new self(...array_slice($matches, 1));
}
/**
* TimeFactory constructor.
* @param int $year
* @param int $weekNumber
* @param int $day
* @param int $hour
* @param int $minute
*/
public function __construct(int $year, int $weekNumber, int $day, int $hour, int $minute)
{
$this->year = $year;
$this->weekNumber = $weekNumber;
$this->day = $day;
$this->hour = $hour;
$this->minute = $minute;
}
/**
* @param int $addedYears
* @return bool
*/
public function addYears(int $addedYears): bool {
$newYears = $this->year + $addedYears;
if($newYears > 9999) {
return false;
} else {
$this->year += $newYears;
}
return true;
}
/**
* @param int $addedWeeks
*/
public function addWeeks(int $addedWeeks): void {
$newWeeks = $this->weekNumber + $addedWeeks;
if($newWeeks >= 52) {
$years = round($newWeeks / 52, 0, PHP_ROUND_HALF_DOWN);
$this->addYears($years);
}
$this->weekNumber += $newWeeks % 52;
}
/**
* @param int $addedDays
*/
public function addDays(int $addedDays): void {
$newDays = $this->day + $addedDays;
if($newDays >= 7) {
$weeks = round($newDays / 7, 0, PHP_ROUND_HALF_DOWN);
$this->addWeeks($weeks);
}
$this->day += $newDays % 7;
}
/**
* @param int $addedHours
*/
public function addHours(int $addedHours): void {
$newHours = $this->hour + $addedHours;
if($newHours >= 24) {
$days = round($newHours / 24, 0, PHP_ROUND_HALF_DOWN);
$this->addDays($days);
}
$this->hour += $newHours % 24;
}
/**
* @param int $addedMinutes
*/
public function addMinutes(int $addedMinutes): void {
$newMinutes = $this->minute + $addedMinutes;
if($newMinutes >= 60) {
$hours = round($newMinutes / 60, 0, PHP_ROUND_HALF_DOWN);
$this->addHours($hours);
}
$this->minute += $newMinutes % 60;
}
/**
* @return int
*/
public function getYears(): int {
return $this->year;
}
/**
* @return int
*/
public function getWeeks(): int {
return $this->weekNumber;
}
/**
* @return int
*/
public function getDays(): int {
return $this->day;
}
/**
* @return int
*/
public function getHours(): int {
return $this->hour;
}
/**
* @return int
*/
public function getMinutes(): int {
return $this->minute;
}
public function reduceTime(Int $amount, String $type) : bool {
switch($type) {
case "m":
if($this->getMinutes() >= $amount) {
$this->minute -= $amount;
return true;
} else {
if($this->getHours() > 0) {
$this->hour -= 1;
$minutesToAdd = 60 - $amount;
$this->addMinutes($minutesToAdd);
return true;
} elseif ($this->getDays() > 0) {
$this->day -= 1;
$hoursToAdd = 23;
$minutesToAdd = 60 - $amount;
$this->addHours($hoursToAdd);
$this->addMinutes($minutesToAdd);
return true;
} elseif ($this->getWeeks() > 0) {
$this->weekNumber -= 1;
$daysToAdd = 6;
$hoursToAdd = 23;
$minutesToAdd = 60 - $amount;
$this->addDays($daysToAdd);
$this->addHours($hoursToAdd);
$this->addMinutes($minutesToAdd);
return true;
} elseif ($this->getYears() > 0) {
$this->year -= 1;
$weeksToAdd = 51;
$daysToAdd = 6;
$hoursToAdd = 23;
$minutesToAdd = 60 - $amount;
$this->addWeeks($weeksToAdd);
$this->addDays($daysToAdd);
$this->addHours($hoursToAdd);
$this->addMinutes($minutesToAdd);
return true;
}
}
break;
case "h":
if($this->getHours() >= $amount) {
$this->hour -= $amount;
return true;
} else {
if ($this->getDays() > 0) {
$this->day -= 1;
$hoursToAdd = 24 - $amount;
$this->addHours($hoursToAdd);
return true;
} elseif ($this->getWeeks() > 0) {
$this->weekNumber -= 1;
$daysToAdd = 6;
$hoursToAdd = 24 - $amount;
$this->addDays($daysToAdd);
$this->addHours($hoursToAdd);
return true;
} elseif ($this->getYears() > 0) {
$this->year -= 1;
$weeksToAdd = 51;
$daysToAdd = 6;
$hoursToAdd = 24 - $amount;
$this->addWeeks($weeksToAdd);
$this->addDays($daysToAdd);
$this->addHours($hoursToAdd);
return true;
}
}
break;
case "d":
if($this->getDays() >= $amount) {
$this->day -= $amount;
return true;
} else {
if ($this->getWeeks() > 0) {
$this->weekNumber--;
$daysToAdd = 7 - $amount;
$this->addDays($daysToAdd);
return true;
} elseif ($this->getYears() > 0) {
$this->year--;
$weeksToAdd = 51;
$daysToAdd = 7 - $amount;
$this->addWeeks($weeksToAdd);
$this->addDays($daysToAdd);
return true;
}
}
break;
case "w":
if($this->getWeeks() >= $amount) {
$this->weekNumber -= $amount;
return true;
} else {
if ($this->getYears() > 0) {
$weeksToAdd = 52 - $amount;
$this->addWeeks($weeksToAdd);
return true;
}
}
break;
case "y":
if($this->getYears() >= $amount) {
$this->year -= $amount;
return true;
}
}
return false;
}
/**
* @return string
*/
public function __toString(): string {
return sprintf('%04d:%02d:%d:%02d:%02d', $this->year, $this->weekNumber, $this->day, $this->hour, $this->minute);
}
}
$factory = TimeFactory::createFromString("0000:00:0:00:00");
$factory->addMinutes(2);
$factory->addMinutes(4);
$factory->addMinutes(6);
$factory->addMinutes(8);
$factory->addMinutes(10);
$factory->addMinutes(12);
echo $factory;
echo "\n" . $factory->getMinutes();
echo "\n" . $factory->getHours();
echo "\n" . $factory->getDays();
echo "\n" . $factory->getWeeks();
echo "\n" . $factory->getYears();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 8.3.6 0.012 0.004 18.56 8.3.5 0.006 0.010 18.42 8.3.4 0.016 0.003 19.17 8.3.3 0.013 0.003 19.05 8.3.2 0.006 0.003 22.02 8.3.1 0.005 0.003 21.89 8.3.0 0.018 0.000 22.45 8.2.18 0.010 0.010 18.79 8.2.17 0.010 0.010 22.96 8.2.16 0.009 0.009 22.10 8.2.15 0.008 0.000 24.18 8.2.14 0.005 0.003 24.66 8.2.13 0.004 0.004 26.16 8.2.12 0.004 0.004 19.73 8.2.11 0.000 0.009 19.52 8.2.10 0.010 0.003 18.16 8.2.9 0.006 0.003 18.13 8.2.8 0.000 0.008 19.45 8.2.7 0.000 0.008 18.00 8.2.6 0.003 0.007 18.00 8.2.5 0.003 0.005 17.88 8.2.4 0.003 0.006 21.27 8.2.3 0.004 0.004 19.52 8.2.2 0.005 0.003 18.27 8.2.1 0.008 0.000 19.32 8.2.0 0.005 0.003 19.50 8.1.28 0.014 0.007 25.92 8.1.27 0.000 0.008 24.04 8.1.26 0.003 0.005 26.35 8.1.25 0.004 0.004 28.09 8.1.24 0.009 0.012 22.36 8.1.23 0.009 0.003 21.01 8.1.22 0.007 0.003 17.91 8.1.21 0.003 0.006 18.95 8.1.20 0.006 0.003 17.60 8.1.19 0.003 0.006 17.60 8.1.18 0.004 0.004 18.10 8.1.17 0.000 0.008 20.89 8.1.16 0.008 0.000 19.21 8.1.15 0.003 0.006 19.18 8.1.14 0.000 0.008 19.12 8.1.13 0.000 0.007 20.27 8.1.12 0.007 0.003 17.87 8.1.11 0.003 0.006 17.71 8.1.10 0.004 0.004 17.79 8.1.9 0.004 0.004 17.78 8.1.8 0.003 0.006 17.76 8.1.7 0.003 0.008 17.80 8.1.6 0.004 0.004 17.85 8.1.5 0.003 0.005 17.86 8.1.4 0.000 0.009 17.88 8.1.3 0.004 0.004 17.85 8.1.2 0.000 0.008 17.98 8.1.1 0.004 0.004 17.92 8.1.0 0.006 0.006 17.87 8.0.30 0.004 0.004 20.21 8.0.29 0.000 0.008 17.13 8.0.28 0.004 0.004 18.56 8.0.27 0.004 0.004 17.39 8.0.26 0.004 0.004 20.28 8.0.25 0.000 0.007 17.38 8.0.24 0.004 0.004 17.42 8.0.23 0.005 0.002 17.24 8.0.22 0.003 0.003 17.16 8.0.21 0.005 0.002 17.30 8.0.20 0.004 0.004 17.33 8.0.19 0.000 0.007 17.36 8.0.18 0.008 0.000 17.38 8.0.17 0.004 0.004 17.23 8.0.16 0.004 0.004 17.38 8.0.15 0.003 0.005 17.23 8.0.14 0.000 0.009 17.29 8.0.13 0.007 0.000 13.70 8.0.12 0.006 0.003 17.24 8.0.11 0.004 0.004 17.22 8.0.10 0.005 0.003 17.30 8.0.9 0.004 0.004 17.31 8.0.8 0.007 0.010 17.30 8.0.7 0.005 0.003 17.17 8.0.6 0.003 0.005 17.27 8.0.5 0.000 0.007 17.12 8.0.3 0.015 0.003 17.49 8.0.2 0.020 0.000 17.56 8.0.1 0.005 0.002 17.53 8.0.0 0.007 0.017 17.21 7.4.33 0.005 0.000 15.55 7.4.32 0.000 0.006 16.90 7.4.30 0.000 0.007 16.80 7.4.29 0.008 0.000 16.90 7.4.28 0.003 0.005 16.82 7.4.27 0.000 0.007 16.99 7.4.26 0.003 0.003 13.68 7.4.25 0.008 0.000 16.78 7.4.24 0.004 0.004 16.98 7.4.23 0.000 0.007 16.84 7.4.22 0.008 0.000 16.74 7.4.21 0.004 0.013 17.04 7.4.20 0.004 0.004 17.02 7.4.13 0.009 0.012 16.86 7.4.12 0.015 0.003 17.03 7.4.11 0.003 0.014 16.95 7.4.10 0.006 0.016 16.74 7.4.9 0.010 0.010 16.92 7.4.8 0.014 0.007 19.39 7.4.7 0.004 0.013 16.91 7.4.6 0.004 0.015 16.85 7.4.5 0.009 0.003 16.67 7.4.4 0.013 0.005 16.95 7.3.33 0.003 0.003 16.71 7.3.32 0.000 0.005 13.42 7.3.31 0.008 0.000 16.62 7.3.30 0.003 0.003 16.62 7.3.29 0.004 0.004 16.70 7.3.28 0.012 0.006 16.66 7.3.26 0.012 0.007 16.81 7.3.24 0.012 0.008 16.82 7.3.23 0.010 0.007 16.95 7.3.21 0.006 0.012 17.02 7.3.20 0.007 0.010 16.74 7.3.19 0.014 0.003 16.79 7.3.18 0.000 0.020 16.70 7.3.17 0.014 0.009 16.55 7.3.16 0.007 0.010 16.75 7.3.11 0.011 0.004 15.33 7.3.10 0.007 0.007 14.95 7.3.9 0.000 0.015 15.14 7.3.8 0.009 0.006 15.33 7.3.7 0.003 0.010 15.16 7.3.6 0.006 0.006 15.10 7.3.5 0.011 0.004 15.17 7.3.4 0.000 0.013 14.99 7.3.3 0.008 0.006 15.01 7.3.2 0.011 0.004 15.07 7.3.1 0.007 0.007 15.08 7.3.0 0.015 0.000 15.14 7.2.33 0.009 0.009 16.88 7.2.32 0.014 0.004 16.91 7.2.31 0.013 0.003 17.14 7.2.30 0.012 0.010 17.18 7.2.29 0.016 0.008 17.12 7.2.24 0.012 0.003 15.42 7.2.23 0.003 0.012 15.39 7.2.22 0.006 0.009 15.20 7.2.21 0.004 0.011 15.52 7.2.20 0.003 0.017 15.27 7.2.19 0.003 0.010 15.05 7.2.18 0.004 0.011 15.07 7.2.17 0.007 0.007 15.14 7.2.16 0.010 0.003 15.20 7.2.15 0.007 0.020 15.41 7.2.14 0.007 0.007 15.33 7.2.13 0.007 0.007 15.30 7.2.12 0.012 0.006 15.38 7.2.11 0.003 0.017 15.22 7.2.10 0.010 0.010 15.27 7.2.9 0.008 0.011 15.50 7.2.8 0.009 0.006 15.50 7.2.7 0.007 0.007 15.54 7.2.6 0.003 0.011 15.24 7.2.5 0.013 0.006 15.31 7.2.4 0.012 0.009 15.52 7.2.3 0.012 0.012 15.23 7.2.2 0.004 0.013 15.31 7.2.1 0.000 0.015 15.49 7.2.0 0.011 0.004 15.32 7.1.33 0.008 0.005 14.28 7.1.32 0.009 0.009 13.97 7.1.31 0.006 0.008 14.17 7.1.30 0.000 0.014 14.35 7.1.29 0.003 0.010 14.34 7.1.28 0.003 0.009 14.27 7.1.27 0.007 0.007 14.24 7.1.26 0.007 0.007 14.22 7.1.25 0.011 0.004 14.14
preferences:dark mode live preview
80.03 ms | 400 KiB | 5 Q