Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 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 All @@ -130,6 +136,7 @@ class Queue extends \Magento\Framework\Model\AbstractModel implements TemplateTy
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
* @param array $data
* @param TimezoneInterface $timezone
* @param LocalizedDateToUtcConverterInterface $utcConverter
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -144,7 +151,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 +167,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 +205,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
40 changes: 14 additions & 26 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 All @@ -257,13 +257,7 @@ public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
}

/**
* @param string|\DateTimeInterface $date
* @param int $dateType
* @param int $timeType
* @param string|null $locale
* @param string|null $timezone
* @param string|null $pattern
* @return string
* @inheritdoc
*/
public function formatDateTime(
$date,
Expand Down Expand Up @@ -299,13 +293,7 @@ public function formatDateTime(
}

/**
* Convert date from config timezone to Utc.
* If pass \DateTime object as argument be sure that timezone is the same with config timezone
*
* @param string|\DateTimeInterface $date
* @param string $format
* @throws LocalizedException
* @return string
* @inheritdoc
*/
public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')
{
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 TimezoneInterface $timezone
* @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,22 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Framework\Stdlib\DateTime\Timezone;

/*
* Interface for converting localized date to UTC
*/
interface LocalizedDateToUtcConverterInterface
{
/**
* Convert localized date to UTC
*
* @param string $date
* @return string
*/
public function convertLocalizedDateToUtc($date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Magento\Framework\Stdlib\DateTime;

use Magento\Framework\Exception\LocalizedException;

/**
* Timezone Interface
* @api
Expand Down Expand Up @@ -80,6 +82,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 Expand Up @@ -121,6 +124,8 @@ public function getConfigTimezone($scopeType = null, $scopeCode = null);
public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null);

/**
* Format date according to date and time formats, locale, timezone and pattern.
*
* @param string|\DateTimeInterface $date
* @param int $dateType
* @param int $timeType
Expand All @@ -139,9 +144,14 @@ public function formatDateTime(
);

/**
* Convert date from config timezone to UTC.
*
* If pass \DateTime object as argument be sure that timezone is the same with config timezone
*
* @param string|\DateTimeInterface $date
* @param string $format
* @return string
* @throws LocalizedException
* @since 100.1.0
*/
public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s');
Expand Down