Skip to content

Commit

Permalink
Merge pull request #3775 from magento-tsg-csl3/2.3-develop-pr16
Browse files Browse the repository at this point in the history
[TSG-CSL3] For 2.3 (pr16)
  • Loading branch information
viktym authored Feb 20, 2019
2 parents 10f900e + de9c950 commit 310648c
Show file tree
Hide file tree
Showing 12 changed files with 835 additions and 136 deletions.
30 changes: 21 additions & 9 deletions app/code/Magento/Cron/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\Exception\CronException;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\Intl\DateTimeFactory;

/**
* Crontab schedule model
Expand Down Expand Up @@ -50,24 +51,32 @@ class Schedule extends \Magento\Framework\Model\AbstractModel
*/
private $timezoneConverter;

/**
* @var DateTimeFactory
*/
private $dateTimeFactory;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param TimezoneInterface $timezoneConverter
* @param TimezoneInterface|null $timezoneConverter
* @param DateTimeFactory|null $dateTimeFactory
*/
public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
TimezoneInterface $timezoneConverter = null
TimezoneInterface $timezoneConverter = null,
DateTimeFactory $dateTimeFactory = null
) {
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
$this->timezoneConverter = $timezoneConverter ?: ObjectManager::getInstance()->get(TimezoneInterface::class);
$this->dateTimeFactory = $dateTimeFactory ?: ObjectManager::getInstance()->get(DateTimeFactory::class);
}

/**
Expand Down Expand Up @@ -111,17 +120,20 @@ public function trySchedule()
if (!$e || !$time) {
return false;
}
$configTimeZone = $this->timezoneConverter->getConfigTimezone();
$storeDateTime = $this->dateTimeFactory->create(null, new \DateTimeZone($configTimeZone));
if (!is_numeric($time)) {
//convert time from UTC to admin store timezone
//we assume that all schedules in configuration (crontab.xml and DB tables) are in admin store timezone
$time = $this->timezoneConverter->date($time)->format('Y-m-d H:i');
$time = strtotime($time);
$dateTimeUtc = $this->dateTimeFactory->create($time);
$time = $dateTimeUtc->getTimestamp();
}
$match = $this->matchCronExpression($e[0], strftime('%M', $time))
&& $this->matchCronExpression($e[1], strftime('%H', $time))
&& $this->matchCronExpression($e[2], strftime('%d', $time))
&& $this->matchCronExpression($e[3], strftime('%m', $time))
&& $this->matchCronExpression($e[4], strftime('%w', $time));
$time = $storeDateTime->setTimestamp($time);
$match = $this->matchCronExpression($e[0], $time->format('i'))
&& $this->matchCronExpression($e[1], $time->format('H'))
&& $this->matchCronExpression($e[2], $time->format('d'))
&& $this->matchCronExpression($e[3], $time->format('m'))
&& $this->matchCronExpression($e[4], $time->format('w'));

return $match;
}
Expand Down
Loading

0 comments on commit 310648c

Please sign in to comment.