Skip to content

Commit 502e5ac

Browse files
committed
New class and interface created for time format convertion
1 parent c6c2ffe commit 502e5ac

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@
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>
297303
<type name="Magento\Framework\Locale\Resolver">
298304
<arguments>
299305
<argument name="defaultLocalePath" xsi:type="const">Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE</argument>

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" />
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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\Locale\ResolverInterface;
11+
use Magento\Framework\App\Config\ScopeConfigInterface;
12+
13+
/**
14+
* Class LocalizedDateToUtcConverter
15+
*/
16+
class LocalizedDateToUtcConverter implements LocalizedDateToUtcConverterInterface
17+
{
18+
/**
19+
* Contains default date format
20+
*
21+
* @var string
22+
*/
23+
private $defaultFormat = 'Y-m-d H:i:s';
24+
25+
/**
26+
* @var ResolverInterface
27+
*/
28+
private $localeResolver;
29+
30+
/**
31+
* @var ScopeConfigInterface
32+
*/
33+
private $scopeConfig;
34+
35+
/**
36+
* @var string
37+
*/
38+
private $scopeType;
39+
40+
/**
41+
* @var string
42+
*/
43+
private $defaultTimezonePath;
44+
45+
/**
46+
* LocalizedDateToUtcConverter constructor.
47+
*
48+
* @param ResolverInterface $localeResolver
49+
*/
50+
public function __construct(
51+
ResolverInterface $localeResolver,
52+
ScopeConfigInterface $scopeConfig,
53+
$scopeType,
54+
$defaultTimezonePath
55+
)
56+
{
57+
$this->localeResolver = $localeResolver;
58+
$this->scopeConfig = $scopeConfig;
59+
$this->scopeType = $scopeType;
60+
$this->defaultTimezonePath = $defaultTimezonePath;
61+
}
62+
63+
/**
64+
* @inheritdoc
65+
*/
66+
public function convertLocalizedDateToUtc($date)
67+
{
68+
$locale = $this->localeResolver->getLocale();
69+
$formatter = new \IntlDateFormatter(
70+
$locale,
71+
\IntlDateFormatter::MEDIUM,
72+
\IntlDateFormatter::MEDIUM,
73+
$this->getConfigTimezone(),
74+
null,
75+
null
76+
);
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()));
81+
82+
$date->setTimezone(new \DateTimeZone('UTC'));
83+
84+
return $date->format($this->defaultFormat);
85+
}
86+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
interface LocalizedDateToUtcConverterInterface
11+
{
12+
/**
13+
* @param string $data
14+
* @return string
15+
*/
16+
public function convertLocalizedDateToUtc($date);
17+
}

0 commit comments

Comments
 (0)