Skip to content

Commit 8435af3

Browse files
author
Stanislav Idolov
authored
ENGCOM-3256: Convert string to DateTime object for languages other than English #18083
2 parents 15e2a28 + 04fe4d8 commit 8435af3

File tree

6 files changed

+135
-31
lines changed

6 files changed

+135
-31
lines changed

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

Lines changed: 14 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
@@ -130,6 +136,7 @@ class Queue extends \Magento\Framework\Model\AbstractModel implements TemplateTy
130136
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
131137
* @param array $data
132138
* @param TimezoneInterface $timezone
139+
* @param LocalizedDateToUtcConverterInterface $utcConverter
133140
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
134141
*/
135142
public function __construct(
@@ -144,7 +151,8 @@ public function __construct(
144151
\Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
145152
\Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
146153
array $data = [],
147-
TimezoneInterface $timezone = null
154+
TimezoneInterface $timezone = null,
155+
LocalizedDateToUtcConverterInterface $utcConverter = null
148156
) {
149157
parent::__construct(
150158
$context,
@@ -159,9 +167,10 @@ public function __construct(
159167
$this->_problemFactory = $problemFactory;
160168
$this->_subscribersCollection = $subscriberCollectionFactory->create();
161169
$this->_transportBuilder = $transportBuilder;
162-
$this->timezone = $timezone ?: \Magento\Framework\App\ObjectManager::getInstance()->get(
163-
TimezoneInterface::class
164-
);
170+
171+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
172+
$this->timezone = $timezone ?: $objectManager->get(TimezoneInterface::class);
173+
$this->utcConverter = $utcConverter ?? $objectManager->get(LocalizedDateToUtcConverterInterface::class);
165174
}
166175

167176
/**
@@ -196,7 +205,7 @@ public function setQueueStartAtByString($startAt)
196205
if ($startAt === null || $startAt == '') {
197206
$this->setQueueStartAt(null);
198207
} else {
199-
$this->setQueueStartAt($this->timezone->convertConfigTimeToUtc($startAt));
208+
$this->setQueueStartAt($this->utcConverter->convertLocalizedDateToUtc($startAt));
200209
}
201210
return $this;
202211
}

app/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
<preference for="Magento\Framework\Locale\FormatInterface" type="Magento\Framework\Locale\Format" />
147147
<preference for="Magento\Framework\Locale\ResolverInterface" type="Magento\Framework\Locale\Resolver" />
148148
<preference for="Magento\Framework\Stdlib\DateTime\TimezoneInterface" type="Magento\Framework\Stdlib\DateTime\Timezone" />
149+
<preference for="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverterInterface" type="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverter" />
149150
<preference for="Magento\Framework\Communication\ConfigInterface" type="Magento\Framework\Communication\Config" />
150151
<preference for="Magento\Framework\Module\ResourceInterface" type="Magento\Framework\Module\ModuleResource" />
151152
<preference for="Magento\Framework\Pricing\Amount\AmountInterface" type="Magento\Framework\Pricing\Amount\Base" />

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

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ public function __construct(
8585
}
8686

8787
/**
88-
* {@inheritdoc}
88+
* @inheritdoc
8989
*/
9090
public function getDefaultTimezonePath()
9191
{
9292
return $this->_defaultTimezonePath;
9393
}
9494

9595
/**
96-
* {@inheritdoc}
96+
* @inheritdoc
9797
*/
9898
public function getDefaultTimezone()
9999
{
100100
return 'UTC';
101101
}
102102

103103
/**
104-
* {@inheritdoc}
104+
* @inheritdoc
105105
*/
106106
public function getConfigTimezone($scopeType = null, $scopeCode = null)
107107
{
@@ -113,7 +113,7 @@ public function getConfigTimezone($scopeType = null, $scopeCode = null)
113113
}
114114

115115
/**
116-
* {@inheritdoc}
116+
* @inheritdoc
117117
*/
118118
public function getDateFormat($type = \IntlDateFormatter::SHORT)
119119
{
@@ -125,7 +125,7 @@ public function getDateFormat($type = \IntlDateFormatter::SHORT)
125125
}
126126

127127
/**
128-
* {@inheritdoc}
128+
* @inheritdoc
129129
*/
130130
public function getDateFormatWithLongYear()
131131
{
@@ -137,7 +137,7 @@ public function getDateFormatWithLongYear()
137137
}
138138

139139
/**
140-
* {@inheritdoc}
140+
* @inheritdoc
141141
*/
142142
public function getTimeFormat($type = \IntlDateFormatter::SHORT)
143143
{
@@ -149,15 +149,15 @@ public function getTimeFormat($type = \IntlDateFormatter::SHORT)
149149
}
150150

151151
/**
152-
* {@inheritdoc}
152+
* @inheritdoc
153153
*/
154154
public function getDateTimeFormat($type)
155155
{
156156
return $this->getDateFormat($type) . ' ' . $this->getTimeFormat($type);
157157
}
158158

159159
/**
160-
* {@inheritdoc}
160+
* @inheritdoc
161161
*/
162162
public function date($date = null, $locale = null, $useTimezone = true, $includeTime = true)
163163
{
@@ -191,7 +191,7 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
191191
}
192192

193193
/**
194-
* {@inheritdoc}
194+
* @inheritdoc
195195
*/
196196
public function scopeDate($scope = null, $date = null, $includeTime = false)
197197
{
@@ -204,7 +204,7 @@ public function scopeDate($scope = null, $date = null, $includeTime = false)
204204
}
205205

206206
/**
207-
* {@inheritdoc}
207+
* @inheritdoc
208208
*/
209209
public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $showTime = false)
210210
{
@@ -218,7 +218,7 @@ public function formatDate($date = null, $format = \IntlDateFormatter::SHORT, $s
218218
}
219219

220220
/**
221-
* {@inheritdoc}
221+
* @inheritdoc
222222
*/
223223
public function scopeTimeStamp($scope = null)
224224
{
@@ -231,7 +231,7 @@ public function scopeTimeStamp($scope = null)
231231
}
232232

233233
/**
234-
* {@inheritdoc}
234+
* @inheritdoc
235235
*/
236236
public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
237237
{
@@ -257,13 +257,7 @@ public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
257257
}
258258

259259
/**
260-
* @param string|\DateTimeInterface $date
261-
* @param int $dateType
262-
* @param int $timeType
263-
* @param string|null $locale
264-
* @param string|null $timezone
265-
* @param string|null $pattern
266-
* @return string
260+
* @inheritdoc
267261
*/
268262
public function formatDateTime(
269263
$date,
@@ -299,13 +293,7 @@ public function formatDateTime(
299293
}
300294

301295
/**
302-
* Convert date from config timezone to Utc.
303-
* If pass \DateTime object as argument be sure that timezone is the same with config timezone
304-
*
305-
* @param string|\DateTimeInterface $date
306-
* @param string $format
307-
* @throws LocalizedException
308-
* @return string
296+
* @inheritdoc
309297
*/
310298
public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')
311299
{
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Stdlib\DateTime\Timezone;
9+
10+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
11+
use Magento\Framework\Locale\ResolverInterface;
12+
use Magento\Framework\App\Config\ScopeConfigInterface;
13+
14+
/**
15+
* Class LocalizedDateToUtcConverter
16+
*/
17+
class LocalizedDateToUtcConverter implements LocalizedDateToUtcConverterInterface
18+
{
19+
/**
20+
* Contains default date format
21+
*
22+
* @var string
23+
*/
24+
private $defaultFormat = 'Y-m-d H:i:s';
25+
26+
/**
27+
* @var TimezoneInterface
28+
*/
29+
private $timezone;
30+
31+
/**
32+
* @var ResolverInterface
33+
*/
34+
private $localeResolver;
35+
36+
/**
37+
* LocalizedDateToUtcConverter constructor.
38+
*
39+
* @param TimezoneInterface $timezone
40+
* @param ResolverInterface $localeResolver
41+
*/
42+
public function __construct(
43+
TimezoneInterface $timezone,
44+
ResolverInterface $localeResolver
45+
) {
46+
$this->timezone = $timezone;
47+
$this->localeResolver = $localeResolver;
48+
}
49+
50+
/**
51+
* @inheritdoc
52+
*/
53+
public function convertLocalizedDateToUtc($date)
54+
{
55+
$configTimezone = $this->timezone->getConfigTimezone();
56+
$locale = $this->localeResolver->getLocale();
57+
58+
$formatter = new \IntlDateFormatter(
59+
$locale,
60+
\IntlDateFormatter::MEDIUM,
61+
\IntlDateFormatter::MEDIUM,
62+
$configTimezone
63+
);
64+
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));
70+
$date->setTimezone(new \DateTimeZone('UTC'));
71+
72+
return $date->format($this->defaultFormat);
73+
}
74+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Framework\Stdlib\DateTime\Timezone;
9+
10+
/*
11+
* Interface for converting localized date to UTC
12+
*/
13+
interface LocalizedDateToUtcConverterInterface
14+
{
15+
/**
16+
* Convert localized date to UTC
17+
*
18+
* @param string $date
19+
* @return string
20+
*/
21+
public function convertLocalizedDateToUtc($date);
22+
}

lib/internal/Magento/Framework/Stdlib/DateTime/TimezoneInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
namespace Magento\Framework\Stdlib\DateTime;
88

9+
use Magento\Framework\Exception\LocalizedException;
10+
911
/**
1012
* Timezone Interface
1113
* @api
@@ -80,6 +82,7 @@ public function scopeDate($scope = null, $date = null, $includeTime = false);
8082

8183
/**
8284
* Get scope timestamp
85+
*
8386
* Timestamp will be built with scope timezone settings
8487
*
8588
* @param mixed $scope
@@ -121,6 +124,8 @@ public function getConfigTimezone($scopeType = null, $scopeCode = null);
121124
public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null);
122125

123126
/**
127+
* Format date according to date and time formats, locale, timezone and pattern.
128+
*
124129
* @param string|\DateTimeInterface $date
125130
* @param int $dateType
126131
* @param int $timeType
@@ -139,9 +144,14 @@ public function formatDateTime(
139144
);
140145

141146
/**
147+
* Convert date from config timezone to UTC.
148+
*
149+
* If pass \DateTime object as argument be sure that timezone is the same with config timezone
150+
*
142151
* @param string|\DateTimeInterface $date
143152
* @param string $format
144153
* @return string
154+
* @throws LocalizedException
145155
* @since 100.1.0
146156
*/
147157
public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s');

0 commit comments

Comments
 (0)