|
9 | 9 | use Magento\Framework\Exception\CronException;
|
10 | 10 | use Magento\Framework\App\ObjectManager;
|
11 | 11 | use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
|
| 12 | +use Magento\Framework\Intl\DateTimeFactory; |
12 | 13 |
|
13 | 14 | /**
|
14 | 15 | * Crontab schedule model
|
@@ -50,24 +51,32 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
|
50 | 51 | */
|
51 | 52 | private $timezoneConverter;
|
52 | 53 |
|
| 54 | + /** |
| 55 | + * @var DateTimeFactory |
| 56 | + */ |
| 57 | + private $dateTimeFactory; |
| 58 | + |
53 | 59 | /**
|
54 | 60 | * @param \Magento\Framework\Model\Context $context
|
55 | 61 | * @param \Magento\Framework\Registry $registry
|
56 | 62 | * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
|
57 | 63 | * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
|
58 | 64 | * @param array $data
|
59 |
| - * @param TimezoneInterface $timezoneConverter |
| 65 | + * @param TimezoneInterface|null $timezoneConverter |
| 66 | + * @param DateTimeFactory|null $dateTimeFactory |
60 | 67 | */
|
61 | 68 | public function __construct(
|
62 | 69 | \Magento\Framework\Model\Context $context,
|
63 | 70 | \Magento\Framework\Registry $registry,
|
64 | 71 | \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
|
65 | 72 | \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
|
66 | 73 | array $data = [],
|
67 |
| - TimezoneInterface $timezoneConverter = null |
| 74 | + TimezoneInterface $timezoneConverter = null, |
| 75 | + DateTimeFactory $dateTimeFactory = null |
68 | 76 | ) {
|
69 | 77 | parent::__construct($context, $registry, $resource, $resourceCollection, $data);
|
70 | 78 | $this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
|
| 79 | + $this->dateTimeFactory = $dateTimeFactory ?: ObjectManager::getInstance()->get(DateTimeFactory::class); |
71 | 80 | }
|
72 | 81 |
|
73 | 82 | /**
|
@@ -111,17 +120,20 @@ public function trySchedule()
|
111 | 120 | if (!$e || !$time) {
|
112 | 121 | return false;
|
113 | 122 | }
|
| 123 | + $configTimeZone = $this->timezoneConverter->getConfigTimezone(); |
| 124 | + $storeDateTime = $this->dateTimeFactory->create(null, new \DateTimeZone($configTimeZone)); |
114 | 125 | if (!is_numeric($time)) {
|
115 | 126 | //convert time from UTC to admin store timezone
|
116 | 127 | //we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
|
117 |
| - $time = $this->timezoneConverter->date($time)->format('Y-m-d H:i'); |
118 |
| - $time = strtotime($time); |
| 128 | + $dateTimeUtc = $this->dateTimeFactory->create($time); |
| 129 | + $time = $dateTimeUtc->getTimestamp(); |
119 | 130 | }
|
120 |
| - $match = $this->matchCronExpression($e[0], strftime('%M', $time)) |
121 |
| - && $this->matchCronExpression($e[1], strftime('%H', $time)) |
122 |
| - && $this->matchCronExpression($e[2], strftime('%d', $time)) |
123 |
| - && $this->matchCronExpression($e[3], strftime('%m', $time)) |
124 |
| - && $this->matchCronExpression($e[4], strftime('%w', $time)); |
| 131 | + $time = $storeDateTime->setTimestamp($time); |
| 132 | + $match = $this->matchCronExpression($e[0], $time->format('i')) |
| 133 | + && $this->matchCronExpression($e[1], $time->format('H')) |
| 134 | + && $this->matchCronExpression($e[2], $time->format('d')) |
| 135 | + && $this->matchCronExpression($e[3], $time->format('m')) |
| 136 | + && $this->matchCronExpression($e[4], $time->format('w')); |
125 | 137 |
|
126 | 138 | return $match;
|
127 | 139 | }
|
|
0 commit comments