-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New class and interface created for time format convertion
- Loading branch information
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
lib/internal/Magento/Framework/Stdlib/DateTime/Timezone/LocalizedDateToUtcConverter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ernal/Magento/Framework/Stdlib/DateTime/Timezone/LocalizedDateToUtcConverterInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |