| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Cron; |
| 6 |
|
| 7 |
use DateTimeInterface; |
| 8 |
use DateTimeZone; |
| 9 |
|
| 10 |
/** |
| 11 |
* Hours field. Allows: * , / -. |
| 12 |
*/ |
| 13 |
class HoursField extends AbstractField |
| 14 |
{ |
| 15 |
/** |
| 16 |
* {@inheritdoc} |
| 17 |
*/ |
| 18 |
protected $rangeStart = 0; |
| 19 |
|
| 20 |
/** |
| 21 |
* {@inheritdoc} |
| 22 |
*/ |
| 23 |
protected $rangeEnd = 23; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var list<array<string, bool|int|string>>|null Transitions returned by DateTimeZone::getTransitions() |
| 27 |
*/ |
| 28 |
protected $transitions = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* @var int|null Timestamp of the start of the transitions range |
| 32 |
*/ |
| 33 |
protected $transitionsStart = null; |
| 34 |
|
| 35 |
/** |
| 36 |
* @var int|null Timestamp of the end of the transitions range |
| 37 |
*/ |
| 38 |
protected $transitionsEnd = null; |
| 39 |
|
| 40 |
/** |
| 41 |
* {@inheritdoc} |
| 42 |
*/ |
| 43 |
public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bool |
| 44 |
{ |
| 45 |
$checkValue = (int) $date->format('H'); |
| 46 |
$retval = $this->isSatisfied($checkValue, $value); |
| 47 |
if ($retval) { |
| 48 |
return $retval; |
| 49 |
} |
| 50 |
|
| 51 |
// Are we on the edge of a transition |
| 52 |
$lastTransition = $this->getPastTransition($date); |
| 53 |
if (($lastTransition !== null) && ($lastTransition["ts"] > ((int) $date->format('U') - 3600))) { |
| 54 |
$dtLastOffset = clone $date; |
| 55 |
$this->timezoneSafeModify($dtLastOffset, "-1 hour"); |
| 56 |
$lastOffset = $dtLastOffset->getOffset(); |
| 57 |
|
| 58 |
$dtNextOffset = clone $date; |
| 59 |
$this->timezoneSafeModify($dtNextOffset, "+1 hour"); |
| 60 |
$nextOffset = $dtNextOffset->getOffset(); |
| 61 |
|
| 62 |
$offsetChange = $nextOffset - $lastOffset; |
| 63 |
if ($offsetChange >= 3600) { |
| 64 |
$checkValue -= 1; |
| 65 |
return $this->isSatisfied($checkValue, $value); |
| 66 |
} |
| 67 |
if ((! $invert) && ($offsetChange <= -3600)) { |
| 68 |
$checkValue += 1; |
| 69 |
return $this->isSatisfied($checkValue, $value); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return $retval; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* @return non-empty-array<string, bool|int|string>|null |
| 78 |
*/ |
| 79 |
public function getPastTransition(DateTimeInterface $date): ?array |
| 80 |
{ |
| 81 |
$currentTimestamp = (int) $date->format('U'); |
| 82 |
if ( |
| 83 |
($this->transitions === null) |
| 84 |
|| ($this->transitionsStart < ($currentTimestamp + 86400)) |
| 85 |
|| ($this->transitionsEnd > ($currentTimestamp - 86400)) |
| 86 |
) { |
| 87 |
// We start a day before current time so we can differentiate between the first transition entry |
| 88 |
// and a change that happens now |
| 89 |
$dtLimitStart = clone $date; |
| 90 |
$dtLimitStart = $dtLimitStart->modify("-12 months"); |
| 91 |
$dtLimitEnd = clone $date; |
| 92 |
$dtLimitEnd = $dtLimitEnd->modify('+12 months'); |
| 93 |
|
| 94 |
$this->transitions = $date->getTimezone()->getTransitions( |
| 95 |
$dtLimitStart->getTimestamp(), |
| 96 |
$dtLimitEnd->getTimestamp() |
| 97 |
); |
| 98 |
if (empty($this->transitions)) { |
| 99 |
return null; |
| 100 |
} |
| 101 |
$this->transitionsStart = $dtLimitStart->getTimestamp(); |
| 102 |
$this->transitionsEnd = $dtLimitEnd->getTimestamp(); |
| 103 |
} |
| 104 |
|
| 105 |
$nextTransition = null; |
| 106 |
foreach ($this->transitions as $transition) { |
| 107 |
if ($transition["ts"] > $currentTimestamp) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if (($nextTransition !== null) && ($transition["ts"] < $nextTransition["ts"])) { |
| 112 |
continue; |
| 113 |
} |
| 114 |
|
| 115 |
$nextTransition = $transition; |
| 116 |
} |
| 117 |
|
| 118 |
return ($nextTransition ?? null); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* {@inheritdoc} |
| 123 |
* |
| 124 |
* @param string|null $parts |
| 125 |
*/ |
| 126 |
public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface |
| 127 |
{ |
| 128 |
$originalTimestamp = (int) $date->format('U'); |
| 129 |
|
| 130 |
// Change timezone to UTC temporarily. This will |
| 131 |
// allow us to go back or forwards and hour even |
| 132 |
// if DST will be changed between the hours. |
| 133 |
if (null === $parts || '*' === $parts) { |
| 134 |
if ($invert) { |
| 135 |
$date = $date->sub(new \DateInterval('PT1H')); |
| 136 |
} else { |
| 137 |
$date = $date->add(new \DateInterval('PT1H')); |
| 138 |
} |
| 139 |
|
| 140 |
$date = $this->setTimeHour($date, $invert, $originalTimestamp); |
| 141 |
return $this; |
| 142 |
} |
| 143 |
|
| 144 |
$parts = false !== strpos($parts, ',') ? explode(',', $parts) : [$parts]; |
| 145 |
$hours = []; |
| 146 |
foreach ($parts as $part) { |
| 147 |
$hours = array_merge($hours, $this->getRangeForExpression($part, 23)); |
| 148 |
} |
| 149 |
|
| 150 |
$current_hour = (int) $date->format('H'); |
| 151 |
$position = $invert ? \count($hours) - 1 : 0; |
| 152 |
$countHours = \count($hours); |
| 153 |
if ($countHours > 1) { |
| 154 |
for ($i = 0; $i < $countHours - 1; ++$i) { |
| 155 |
if ((!$invert && $current_hour >= $hours[$i] && $current_hour < $hours[$i + 1]) || |
| 156 |
($invert && $current_hour > $hours[$i] && $current_hour <= $hours[$i + 1])) { |
| 157 |
$position = $invert ? $i : $i + 1; |
| 158 |
|
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
$target = (int) $hours[$position]; |
| 165 |
$originalHour = (int)$date->format('H'); |
| 166 |
$originalDst = (int)$date->format('I'); |
| 167 |
|
| 168 |
$originalDay = (int)$date->format('d'); |
| 169 |
$previousOffset = $date->getOffset(); |
| 170 |
|
| 171 |
if (! $invert) { |
| 172 |
if ($originalHour >= $target) { |
| 173 |
$distance = 24 - $originalHour; |
| 174 |
$date = $this->timezoneSafeModify($date, "+{$distance} hours"); |
| 175 |
|
| 176 |
$actualDay = (int)$date->format('d'); |
| 177 |
$actualHour = (int)$date->format('H'); |
| 178 |
if (($actualDay !== ($originalDay + 1)) && ($actualHour !== 0)) { |
| 179 |
$offsetChange = ($previousOffset - $date->getOffset()); |
| 180 |
$date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds"); |
| 181 |
} |
| 182 |
|
| 183 |
$originalHour = (int)$date->format('H'); |
| 184 |
} |
| 185 |
|
| 186 |
$distance = $target - $originalHour; |
| 187 |
$date = $this->timezoneSafeModify($date, "+{$distance} hours"); |
| 188 |
} else { |
| 189 |
if ($originalHour <= $target) { |
| 190 |
$distance = ($originalHour + 1); |
| 191 |
$date = $this->timezoneSafeModify($date, "-" . $distance . " hours"); |
| 192 |
|
| 193 |
$actualDay = (int)$date->format('d'); |
| 194 |
$actualHour = (int)$date->format('H'); |
| 195 |
if (($actualDay !== ($originalDay - 1)) && ($actualHour !== 23)) { |
| 196 |
$offsetChange = ($previousOffset - $date->getOffset()); |
| 197 |
$date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds"); |
| 198 |
} |
| 199 |
|
| 200 |
$originalHour = (int)$date->format('H'); |
| 201 |
} |
| 202 |
|
| 203 |
$distance = $originalHour - $target; |
| 204 |
$date = $this->timezoneSafeModify($date, "-{$distance} hours"); |
| 205 |
} |
| 206 |
|
| 207 |
$actualDst = (int)$date->format('I'); |
| 208 |
if ($originalDst < $actualDst) { |
| 209 |
$date = $this->timezoneSafeModify($date, "-1 hours"); |
| 210 |
} |
| 211 |
|
| 212 |
$date = $this->setTimeHour($date, $invert, $originalTimestamp); |
| 213 |
|
| 214 |
$actualHour = (int)$date->format('H'); |
| 215 |
if ($invert && ($actualHour === ($target - 1) || (($actualHour === 23) && ($target === 0)))) { |
| 216 |
$date = $this->timezoneSafeModify($date, "+1 hour"); |
| 217 |
} |
| 218 |
|
| 219 |
return $this; |
| 220 |
} |
| 221 |
} |