summaryrefslogtreecommitdiffstats
path: root/vendor/dragonmantank/cron-expression/src/Cron/HoursField.php
blob: 413d138be3fd6cd417c9022f17cd0008b80b00f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php

declare(strict_types=1);

namespace Cron;

use DateTimeInterface;
use DateTimeZone;

/**
 * Hours field.  Allows: * , / -.
 */
class HoursField extends AbstractField
{
    /**
     * {@inheritdoc}
     */
    protected $rangeStart = 0;

    /**
     * {@inheritdoc}
     */
    protected $rangeEnd = 23;

    /**
     * @var array|null Transitions returned by DateTimeZone::getTransitions()
     */
    protected $transitions = [];

    /**
     * @var int|null Timestamp of the start of the transitions range
     */
    protected $transitionsStart = null;

    /**
     * @var int|null Timestamp of the end of the transitions range
     */
    protected $transitionsEnd = null;

    /**
     * {@inheritdoc}
     */
    public function isSatisfiedBy(DateTimeInterface $date, $value, bool $invert): bool
    {
        $checkValue = (int) $date->format('H');
        $retval = $this->isSatisfied($checkValue, $value);
        if ($retval) {
            return $retval;
        }

        // Are we on the edge of a transition
        $lastTransition = $this->getPastTransition($date);
        if (($lastTransition !== null) && ($lastTransition["ts"] > ((int) $date->format('U') - 3600))) {
            $dtLastOffset = clone $date;
            $this->timezoneSafeModify($dtLastOffset, "-1 hour");
            $lastOffset = $dtLastOffset->getOffset();

            $dtNextOffset = clone $date;
            $this->timezoneSafeModify($dtNextOffset, "+1 hour");
            $nextOffset = $dtNextOffset->getOffset();

            $offsetChange = $nextOffset - $lastOffset;
            if ($offsetChange >= 3600) {
                $checkValue -= 1;
                return $this->isSatisfied($checkValue, $value);
            }
            if ((! $invert) && ($offsetChange <= -3600)) {
                $checkValue += 1;
                return $this->isSatisfied($checkValue, $value);
            }
        }

        return $retval;
    }

    public function getPastTransition(DateTimeInterface $date): ?array
    {
        $currentTimestamp = (int) $date->format('U');
        if (
            ($this->transitions === null)
            || ($this->transitionsStart < ($currentTimestamp + 86400))
            || ($this->transitionsEnd > ($currentTimestamp - 86400))
        ) {
            // We start a day before current time so we can differentiate between the first transition entry
            // and a change that happens now
            $dtLimitStart = clone $date;
            $dtLimitStart = $dtLimitStart->modify("-12 months");
            $dtLimitEnd = clone $date;
            $dtLimitEnd = $dtLimitEnd->modify('+12 months');

            $this->transitions = $date->getTimezone()->getTransitions(
                $dtLimitStart->getTimestamp(),
                $dtLimitEnd->getTimestamp()
            );
            if (empty($this->transitions)) {
                return null;
            }
            $this->transitionsStart = $dtLimitStart->getTimestamp();
            $this->transitionsEnd = $dtLimitEnd->getTimestamp();
        }

        $nextTransition = null;
        foreach ($this->transitions as $transition) {
            if ($transition["ts"] > $currentTimestamp) {
                continue;
            }

            if (($nextTransition !== null) && ($transition["ts"] < $nextTransition["ts"])) {
                continue;
            }

            $nextTransition = $transition;
        }

        return ($nextTransition ?? null);
    }

    /**
     * {@inheritdoc}
     *
     * @param string|null                  $parts
     */
    public function increment(DateTimeInterface &$date, $invert = false, $parts = null): FieldInterface
    {
        $originalTimestamp = (int) $date->format('U');

        // Change timezone to UTC temporarily. This will
        // allow us to go back or forwards and hour even
        // if DST will be changed between the hours.
        if (null === $parts || '*' === $parts) {
            if ($invert) {
                $date = $date->sub(new \DateInterval('PT1H'));
            } else {
                $date = $date->add(new \DateInterval('PT1H'));
            }

            $date = $this->setTimeHour($date, $invert, $originalTimestamp);
            return $this;
        }

        $parts = false !== strpos($parts, ',') ? explode(',', $parts) : [$parts];
        $hours = [];
        foreach ($parts as $part) {
            $hours = array_merge($hours, $this->getRangeForExpression($part, 23));
        }

        $current_hour = (int) $date->format('H');
        $position = $invert ? \count($hours) - 1 : 0;
        $countHours = \count($hours);
        if ($countHours > 1) {
            for ($i = 0; $i < $countHours - 1; ++$i) {
                if ((!$invert && $current_hour >= $hours[$i] && $current_hour < $hours[$i + 1]) ||
                    ($invert && $current_hour > $hours[$i] && $current_hour <= $hours[$i + 1])) {
                    $position = $invert ? $i : $i + 1;

                    break;
                }
            }
        }

        $target = (int) $hours[$position];
        $originalHour = (int)$date->format('H');

        $originalDay = (int)$date->format('d');
        $previousOffset = $date->getOffset();

        if (! $invert) {
            if ($originalHour >= $target) {
                $distance = 24 - $originalHour;
                $date = $this->timezoneSafeModify($date, "+{$distance} hours");

                $actualDay = (int)$date->format('d');
                $actualHour = (int)$date->format('H');
                if (($actualDay !== ($originalDay + 1)) && ($actualHour !== 0)) {
                    $offsetChange = ($previousOffset - $date->getOffset());
                    $date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds");
                }

                $originalHour = (int)$date->format('H');
            }

            $distance = $target - $originalHour;
            $date = $this->timezoneSafeModify($date, "+{$distance} hours");
        } else {
            if ($originalHour <= $target) {
                $distance = ($originalHour + 1);
                $date = $this->timezoneSafeModify($date, "-" . $distance . " hours");

                $actualDay = (int)$date->format('d');
                $actualHour = (int)$date->format('H');
                if (($actualDay !== ($originalDay - 1)) && ($actualHour !== 23)) {
                    $offsetChange = ($previousOffset - $date->getOffset());
                    $date = $this->timezoneSafeModify($date, "+{$offsetChange} seconds");
                }

                $originalHour = (int)$date->format('H');
            }

            $distance = $originalHour - $target;
            $date = $this->timezoneSafeModify($date, "-{$distance} hours");
        }

        $date = $this->setTimeHour($date, $invert, $originalTimestamp);

        $actualHour = (int)$date->format('H');
        if ($invert && ($actualHour === ($target - 1) || (($actualHour === 23) && ($target === 0)))) {
            $date = $this->timezoneSafeModify($date, "+1 hour");
        }

        return $this;
    }
}