Skip to content
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions app/code/Magento/Newsletter/Model/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Magento\Framework\App\TemplateTypesInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverterInterface;

/**
* Newsletter queue model.
Expand Down Expand Up @@ -117,6 +118,11 @@ class Queue extends \Magento\Framework\Model\AbstractModel implements TemplateTy
*/
private $timezone;

/**
* @var LocalizedDateToUtcConverterInterface
*/
private $utcConverter;

/**
* @param \Magento\Framework\Model\Context $context
* @param \Magento\Framework\Registry $registry
Expand Down Expand Up @@ -144,7 +150,8 @@ public function __construct(
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
array $data = [],
TimezoneInterface $timezone = null
TimezoneInterface $timezone = null,
LocalizedDateToUtcConverterInterface $utcConverter = null
) {
parent::__construct(
$context,
Expand All @@ -159,9 +166,10 @@ public function __construct(
$this->_problemFactory = $problemFactory;
$this->_subscribersCollection = $subscriberCollectionFactory->create();
$this->_transportBuilder = $transportBuilder;
$this->timezone = $timezone ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
TimezoneInterface::class
);

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$this->timezone = $timezone ?: $objectManager->get(TimezoneInterface::class);
$this->utcConverter = $utcConverter ?? $objectManager->get(LocalizedDateToUtcConverterInterface::class);
}

/**
Expand Down Expand Up @@ -196,7 +204,7 @@ public function setQueueStartAtByString($startAt)
if ($startAt === null || $startAt == '') {
$this->setQueueStartAt(null);
} else {
$this->setQueueStartAt($this->timezone->convertConfigTimeToUtc($startAt));
$this->setQueueStartAt($this->utcConverter->convertLocalizedDateToUtc($startAt));
}
return $this;
}
Expand Down
1 change: 1 addition & 0 deletions app/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
<preference for="Magento\Framework\Locale\FormatInterface" type="Magento\Framework\Locale\Format" />
<preference for="Magento\Framework\Locale\ResolverInterface" type="Magento\Framework\Locale\Resolver" />
<preference for="Magento\Framework\Stdlib\DateTime\TimezoneInterface" type="Magento\Framework\Stdlib\DateTime\Timezone" />
<preference for="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverterInterface" type="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverter" />
<preference for="Magento\Framework\Communication\ConfigInterface" type="Magento\Framework\Communication\Config" />
<preference for="Magento\Framework\Module\ResourceInterface" type="Magento\Framework\Module\ModuleResource" />
<preference for="Magento\Framework\Pricing\Amount\AmountInterface" type="Magento\Framework\Pricing\Amount\Base" />
Expand Down
24 changes: 12 additions & 12 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,23 @@ public function __construct(
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDefaultTimezonePath()
{
return $this->_defaultTimezonePath;
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDefaultTimezone()
{
return 'UTC';
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getConfigTimezone($scopeType = null, $scopeCode = null)
{
Expand All @@ -113,7 +113,7 @@ public function getConfigTimezone($scopeType = null, $scopeCode = null)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDateFormat($type = \IntlDateFormatter::SHORT)
{
Expand All @@ -125,7 +125,7 @@ public function getDateFormat($type = \IntlDateFormatter::SHORT)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDateFormatWithLongYear()
{
Expand All @@ -137,7 +137,7 @@ public function getDateFormatWithLongYear()
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getTimeFormat($type = \IntlDateFormatter::SHORT)
{
Expand All @@ -149,15 +149,15 @@ public function getTimeFormat($type = \IntlDateFormatter::SHORT)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function getDateTimeFormat($type)
{
return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true)
{
Expand Down Expand Up @@ -191,7 +191,7 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function scopeDate($scope = null, $date = null, $includeTime = false)
{
Expand All @@ -204,7 +204,7 @@ public function scopeDate($scope = null, $date = null, $includeTime = false)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $showTime = false)
{
Expand All @@ -218,7 +218,7 @@ public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $s
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function scopeTimeStamp($scope = null)
{
Expand All @@ -231,7 +231,7 @@ public function scopeTimeStamp($scope = null)
}

/**
* {@inheritdoc}
* @inheritdoc
*/
public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Stdlib\DateTime\Timezone;

use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Class LocalizedDateToUtcConverter
*/
class LocalizedDateToUtcConverter implements LocalizedDateToUtcConverterInterface
{
/**
* Contains default date format
*
* @var string
*/
private $defaultFormat = 'Y-m-d H:i:s';

/**
* @var TimezoneInterface
*/
private $timezone;

/**
* @var ResolverInterface
*/
private $localeResolver;

/**
* LocalizedDateToUtcConverter constructor.
*
* @param ResolverInterface $localeResolver
*/
public function __construct(
TimezoneInterface $timezone,
ResolverInterface $localeResolver
)
{
$this->timezone = $timezone;
$this->localeResolver = $localeResolver;
}

/**
* @inheritdoc
*/
public function convertLocalizedDateToUtc($date)
{
$configTimezone = $this->timezone->getConfigTimezone();
$locale = $this->localeResolver->getLocale();

$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
$configTimezone
);

$localTimestamp = $formatter->parse($date);
$gmtTimestamp = $this->timezone->date($localTimestamp)->getTimestamp();
$formattedUniversalTime = date($this->defaultFormat, $gmtTimestamp);

$date = new \DateTime($formattedUniversalTime, new \DateTimeZone($configTimezone));
$date->setTimezone(new \DateTimeZone('UTC'));

return $date->format($this->defaultFormat);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Stdlib\DateTime\Timezone;

interface LocalizedDateToUtcConverterInterface
{
/**
* @param string $data
Comment thread
slavvka marked this conversation as resolved.
Outdated
* @return string
*/
public function convertLocalizedDateToUtc($date);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a new line in the end

Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function scopeDate($scope = null, $date = null, $includeTime = false);

/**
* Get scope timestamp
*
* Timestamp will be built with scope timezone settings
*
* @param mixed $scope
Expand Down