AbstractField.php
1 year ago
CronExpression.php
8 months ago
DayOfMonthField.php
1 year ago
DayOfWeekField.php
1 year ago
FieldFactory.php
1 year ago
FieldFactoryInterface.php
1 year ago
FieldInterface.php
1 year ago
HoursField.php
1 year ago
MinutesField.php
1 year ago
MonthField.php
1 year ago
index.php
1 year ago
HoursField.php
151 lines
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | namespace Cron; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use DateTimeInterface; |
| 6 | use DateTimeZone; |
| 7 | class HoursField extends AbstractField |
| 8 | { |
| 9 | protected $rangeStart = 0; |
| 10 | protected $rangeEnd = 23; |
| 11 | protected $transitions = []; |
| 12 | protected $transitionsStart = null; |
| 13 | protected $transitionsEnd = null; |
| 14 | public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bool |
| 15 | { |
| 16 | $checkValue = (int) $date->format('H'); |
| 17 | $retval = $this->isSatisfied($checkValue, $value); |
| 18 | if ($retval) { |
| 19 | return $retval; |
| 20 | } |
| 21 | // Are we on the edge of a transition |
| 22 | $lastTransition = $this->getPastTransition($date); |
| 23 | if (($lastTransition !== null) && ($lastTransition["ts"] > ((int) $date->format('U') - 3600))) { |
| 24 | $dtLastOffset = clone $date; |
| 25 | $this->timezoneSafeModify($dtLastOffset, "-1 hour"); |
| 26 | $lastOffset = $dtLastOffset->getOffset(); |
| 27 | $dtNextOffset = clone $date; |
| 28 | $this->timezoneSafeModify($dtNextOffset, "+1 hour"); |
| 29 | $nextOffset = $dtNextOffset->getOffset(); |
| 30 | $offsetChange = $nextOffset - $lastOffset; |
| 31 | if ($offsetChange >= 3600) { |
| 32 | $checkValue -= 1; |
| 33 | return $this->isSatisfied($checkValue, $value); |
| 34 | } |
| 35 | if ((! $invert) && ($offsetChange <= -3600)) { |
| 36 | $checkValue += 1; |
| 37 | return $this->isSatisfied($checkValue, $value); |
| 38 | } |
| 39 | } |
| 40 | return $retval; |
| 41 | } |
| 42 | public function getPastTransition(DateTimeInterface $date): ?array |
| 43 | { |
| 44 | $currentTimestamp = (int) $date->format('U'); |
| 45 | if ( |
| 46 | ($this->transitions === null) |
| 47 | || ($this->transitionsStart < ($currentTimestamp + 86400)) |
| 48 | || ($this->transitionsEnd > ($currentTimestamp - 86400)) |
| 49 | ) { |
| 50 | // We start a day before current time so we can differentiate between the first transition entry |
| 51 | // and a change that happens now |
| 52 | $dtLimitStart = clone $date; |
| 53 | $dtLimitStart = $dtLimitStart->modify("-12 months"); |
| 54 | $dtLimitEnd = clone $date; |
| 55 | $dtLimitEnd = $dtLimitEnd->modify('+12 months'); |
| 56 | $this->transitions = $date->getTimezone()->getTransitions( |
| 57 | $dtLimitStart->getTimestamp(), |
| 58 | $dtLimitEnd->getTimestamp() |
| 59 | ); |
| 60 | if (empty($this->transitions)) { |
| 61 | return null; |
| 62 | } |
| 63 | $this->transitionsStart = $dtLimitStart->getTimestamp(); |
| 64 | $this->transitionsEnd = $dtLimitEnd->getTimestamp(); |
| 65 | } |
| 66 | $nextTransition = null; |
| 67 | foreach ($this->transitions as $transition) { |
| 68 | if ($transition["ts"] > $currentTimestamp) { |
| 69 | continue; |
| 70 | } |
| 71 | if (($nextTransition !== null) && ($transition["ts"] < $nextTransition["ts"])) { |
| 72 | continue; |
| 73 | } |
| 74 | $nextTransition = $transition; |
| 75 | } |
| 76 | return ($nextTransition ?? null); |
| 77 | } |
| 78 | public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface |
| 79 | { |
| 80 | $originalTimestamp = (int) $date->format('U'); |
| 81 | // Change timezone to UTC temporarily. This will |
| 82 | // allow us to go back or forwards and hour even |
| 83 | // if DST will be changed between the hours. |
| 84 | if (null === $parts || '*' === $parts) { |
| 85 | if ($invert) { |
| 86 | $date = $date->sub(new \DateInterval('PT1H')); |
| 87 | } else { |
| 88 | $date = $date->add(new \DateInterval('PT1H')); |
| 89 | } |
| 90 | $date = $this->setTimeHour($date, $invert, $originalTimestamp); |
| 91 | return $this; |
| 92 | } |
| 93 | $parts = false !== strpos($parts, ',') ? explode(',', $parts) : [$parts]; |
| 94 | $hours = []; |
| 95 | foreach ($parts as $part) { |
| 96 | $hours = array_merge($hours, $this->getRangeForExpression($part, 23)); |
| 97 | } |
| 98 | $current_hour = (int) $date->format('H'); |
| 99 | $position = $invert ? \count($hours) - 1 : 0; |
| 100 | $countHours = \count($hours); |
| 101 | if ($countHours > 1) { |
| 102 | for ($i = 0; $i < $countHours - 1; ++$i) { |
| 103 | if ((!$invert && $current_hour >= $hours[$i] && $current_hour < $hours[$i + 1]) || |
| 104 | ($invert && $current_hour > $hours[$i] && $current_hour <= $hours[$i + 1])) { |
| 105 | $position = $invert ? $i : $i + 1; |
| 106 | break; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | $target = (int) $hours[$position]; |
| 111 | $originalHour = (int)$date->format('H'); |
| 112 | $originalDay = (int)$date->format('d'); |
| 113 | $previousOffset = $date->getOffset(); |
| 114 | if (! $invert) { |
| 115 | if ($originalHour >= $target) { |
| 116 | $distance = 24 - $originalHour; |
| 117 | $date = $this->timezoneSafeModify($date, "+{$distance} hours"); |
| 118 | $actualDay = (int)$date->format('d'); |
| 119 | $actualHour = (int)$date->format('H'); |
| 120 | if (($actualDay !== ($originalDay + 1)) && ($actualHour !== 0)) { |
| 121 | $offsetChange = ($previousOffset - $date->getOffset()); |
| 122 | $date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds"); |
| 123 | } |
| 124 | $originalHour = (int)$date->format('H'); |
| 125 | } |
| 126 | $distance = $target - $originalHour; |
| 127 | $date = $this->timezoneSafeModify($date, "+{$distance} hours"); |
| 128 | } else { |
| 129 | if ($originalHour <= $target) { |
| 130 | $distance = ($originalHour + 1); |
| 131 | $date = $this->timezoneSafeModify($date, "-" . $distance . " hours"); |
| 132 | $actualDay = (int)$date->format('d'); |
| 133 | $actualHour = (int)$date->format('H'); |
| 134 | if (($actualDay !== ($originalDay - 1)) && ($actualHour !== 23)) { |
| 135 | $offsetChange = ($previousOffset - $date->getOffset()); |
| 136 | $date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds"); |
| 137 | } |
| 138 | $originalHour = (int)$date->format('H'); |
| 139 | } |
| 140 | $distance = $originalHour - $target; |
| 141 | $date = $this->timezoneSafeModify($date, "-{$distance} hours"); |
| 142 | } |
| 143 | $date = $this->setTimeHour($date, $invert, $originalTimestamp); |
| 144 | $actualHour = (int)$date->format('H'); |
| 145 | if ($invert && ($actualHour === ($target - 1) || (($actualHour === 23) && ($target === 0)))) { |
| 146 | $date = $this->timezoneSafeModify($date, "+1 hour"); |
| 147 | } |
| 148 | return $this; |
| 149 | } |
| 150 | } |
| 151 |