Skip to content

Commit

Permalink
New class and interface created for time format convertion
Browse files Browse the repository at this point in the history
  • Loading branch information
bnymn committed Oct 8, 2018
1 parent c6c2ffe commit 502e5ac
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/code/Magento/Store/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
</arguments>
</type>
<type name="Magento\Framework\Stdlib\DateTime\Timezone\LocalizedDateToUtcConverter">
<arguments>
<argument name="defaultTimezonePath" xsi:type="const">Magento\Directory\Helper\Data::XML_PATH_DEFAULT_TIMEZONE</argument>
<argument name="scopeType" xsi:type="const">Magento\Store\Model\ScopeInterface::SCOPE_STORE</argument>
</arguments>
</type>
<type name="Magento\Framework\Locale\Resolver">
<arguments>
<argument name="defaultLocalePath" xsi:type="const">Magento\Directory\Helper\Data::XML_PATH_DEFAULT_LOCALE</argument>
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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\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 ResolverInterface
*/
private $localeResolver;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* @var string
*/
private $scopeType;

/**
* @var string
*/
private $defaultTimezonePath;

/**
* LocalizedDateToUtcConverter constructor.
*
* @param ResolverInterface $localeResolver
*/
public function __construct(
ResolverInterface $localeResolver,
ScopeConfigInterface $scopeConfig,
$scopeType,
$defaultTimezonePath
)
{
$this->localeResolver = $localeResolver;
$this->scopeConfig = $scopeConfig;
$this->scopeType = $scopeType;
$this->defaultTimezonePath = $defaultTimezonePath;
}

/**
* @inheritdoc
*/
public function convertLocalizedDateToUtc($date)
{
$locale = $this->localeResolver->getLocale();
$formatter = new \IntlDateFormatter(
$locale,
\IntlDateFormatter::MEDIUM,
\IntlDateFormatter::MEDIUM,
$this->getConfigTimezone(),
null,
null
);
$unixTime = $formatter->parse($date);
$dateTime = new DateTime($this);
$dateUniversal = $dateTime->gmtDate(null, $unixTime);
$date = new \DateTime($dateUniversal, new \DateTimeZone($this->getConfigTimezone()));

$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
* @return string
*/
public function convertLocalizedDateToUtc($date);
}

0 comments on commit 502e5ac

Please sign in to comment.