Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 51e8fe7

Browse files
committed
Convert local timestamp into GMT timestamp
1 parent 502e5ac commit 51e8fe7

3 files changed

Lines changed: 29 additions & 39 deletions

File tree

app/code/Magento/Newsletter/Model/Queue.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\App\TemplateTypesInterface;
99
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
10+
use Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverterInterface;
1011

1112
/**
1213
* Newsletter queue model.
@@ -117,6 +118,11 @@ class Queue extends \Magento\Framework\Model\AbstractModel implements TemplateTy
117118
*/
118119
private $timezone;
119120

121+
/**
122+
* @var LocalizedDateToUtcConverterInterface
123+
*/
124+
private $utcConverter;
125+
120126
/**
121127
* @param \Magento\Framework\Model\Context $context
122128
* @param \Magento\Framework\Registry $registry
@@ -144,7 +150,8 @@ public function __construct(
144150
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
145151
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
146152
array $data = [],
147-
TimezoneInterface $timezone = null
153+
TimezoneInterface $timezone = null,
154+
LocalizedDateToUtcConverterInterface $utcConverter = null
148155
) {
149156
parent::__construct(
150157
$context,
@@ -159,9 +166,10 @@ public function __construct(
159166
$this->_problemFactory = $problemFactory;
160167
$this->_subscribersCollection = $subscriberCollectionFactory->create();
161168
$this->_transportBuilder = $transportBuilder;
162-
$this->timezone = $timezone ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
163-
TimezoneInterface::class
164-
);
169+
170+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
171+
$this->timezone = $timezone ?: $objectManager->get(TimezoneInterface::class);
172+
$this->utcConverter = $utcConverter ?? $objectManager->get(LocalizedDateToUtcConverterInterface::class);
165173
}
166174

167175
/**
@@ -196,7 +204,7 @@ public function setQueueStartAtByString($startAt)
196204
if ($startAt === null || $startAt == '') {
197205
$this->setQueueStartAt(null);
198206
} else {
199-
$this->setQueueStartAt($this->timezone->convertConfigTimeToUtc($startAt));
207+
$this->setQueueStartAt($this->utcConverter->convertLocalizedDateToUtc($startAt));
200208
}
201209
return $this;
202210
}

app/code/Magento/Store/etc/di.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,6 @@
294294
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
295295
</arguments>
296296
</type>
297-
<type name="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverter">
298-
<arguments>
299-
<argument name="defaultTimezonePath" xsi:type="const">Magento\Directory\Helper\Data::XML_PATH_DEFAULT_TIMEZONE</argument>
300-
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
301-
</arguments>
302-
</type>
303297
<type name="Magento\Framework\Locale\Resolver">
304298
<arguments>
305299
<argument name="defaultLocalePath" xsi:type="const">Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE</argument>

lib/internal/Magento/Framework/Stdlib/DateTime/Timezone/LocalizedDateToUtcConverter.php

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\Framework\Stdlib\DateTime\Timezone;
99

10+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
1011
use Magento\Framework\Locale\ResolverInterface;
1112
use Magento\Framework\App\Config\ScopeConfigInterface;
1213

@@ -23,62 +24,49 @@ class LocalizedDateToUtcConverter implements LocalizedDateToUtcConverterInterfac
2324
private $defaultFormat = 'Y-m-d H:i:s';
2425

2526
/**
26-
* @var ResolverInterface
27-
*/
28-
private $localeResolver;
29-
30-
/**
31-
* @var ScopeConfigInterface
32-
*/
33-
private $scopeConfig;
34-
35-
/**
36-
* @var string
27+
* @var TimezoneInterface
3728
*/
38-
private $scopeType;
29+
private $timezone;
3930

4031
/**
41-
* @var string
32+
* @var ResolverInterface
4233
*/
43-
private $defaultTimezonePath;
34+
private $localeResolver;
4435

4536
/**
4637
* LocalizedDateToUtcConverter constructor.
4738
*
4839
* @param ResolverInterface $localeResolver
4940
*/
5041
public function __construct(
51-
ResolverInterface $localeResolver,
52-
ScopeConfigInterface $scopeConfig,
53-
$scopeType,
54-
$defaultTimezonePath
42+
TimezoneInterface $timezone,
43+
ResolverInterface $localeResolver
5544
)
5645
{
46+
$this->timezone = $timezone;
5747
$this->localeResolver = $localeResolver;
58-
$this->scopeConfig = $scopeConfig;
59-
$this->scopeType = $scopeType;
60-
$this->defaultTimezonePath = $defaultTimezonePath;
6148
}
6249

6350
/**
6451
* @inheritdoc
6552
*/
6653
public function convertLocalizedDateToUtc($date)
6754
{
55+
$configTimezone = $this->timezone->getConfigTimezone();
6856
$locale = $this->localeResolver->getLocale();
57+
6958
$formatter = new \IntlDateFormatter(
7059
$locale,
7160
\IntlDateFormatter::MEDIUM,
7261
\IntlDateFormatter::MEDIUM,
73-
$this->getConfigTimezone(),
74-
null,
75-
null
62+
$configTimezone
7663
);
77-
$unixTime = $formatter->parse($date);
78-
$dateTime = new DateTime($this);
79-
$dateUniversal = $dateTime->gmtDate(null, $unixTime);
80-
$date = new \DateTime($dateUniversal, new \DateTimeZone($this->getConfigTimezone()));
8164

65+
$localTimestamp = $formatter->parse($date);
66+
$gmtTimestamp = $this->timezone->date($localTimestamp)->getTimestamp();
67+
$formattedUniversalTime = date($this->defaultFormat, $gmtTimestamp);
68+
69+
$date = new \DateTime($formattedUniversalTime, new \DateTimeZone($configTimezone));
8270
$date->setTimezone(new \DateTimeZone('UTC'));
8371

8472
return $date->format($this->defaultFormat);

0 commit comments

Comments
 (0)