Skip to content

[Backport-2.2] of PR-#11306 Save date time correctly in different timezone and local #13560

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,15 @@ public function localeDatetimeFilterProvider()
['fr_FR', '01/02/2010 15:30', '2010-02-01 15:30:00'],
['fr_FR', '01/02/2010 1:00', '2010-02-01 01:00:00'],
['fr_FR', '01/02/2010 01:00', '2010-02-01 01:00:00'],
['en_US', '11/28/2010', '2010-11-28 00:00:00'],
['en_US', '11/28/2010 1:00am', '2010-11-28 01:00:00'],
['en_US', '11/28/2010 01:00am', '2010-11-28 01:00:00'],
['es_ES', '28/11/2010', '2010-11-28 00:00:00'],
['es_ES', '28/11/2010 23:12:00', '2010-11-28 23:12:00'],
['es_ES', '28/11/2010 23:12', '2010-11-28 23:12:00'],
['de_DE', '01/02/2010 15:30', '2010-02-01 15:30:00'],
['en_US', '2017-09-01T15:30:00.000Z', '2017-09-01 15:30:00'],
['fr_FR', '2017-09-01T15:30:00.000Z', '2017-09-01 15:30:00'],
['fr_FR', '2017-09-01T15:30:00.000Z', '2017-09-01 15:30:00']
];
}
}
52 changes: 38 additions & 14 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@
*/
namespace Magento\Framework\Stdlib\DateTime;

use \Magento\Framework\Exception\LocalizedException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\ScopeInterface;
use Magento\Framework\App\ScopeResolverInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Locale\ResolverInterface;
use Magento\Framework\Phrase;
use Magento\Framework\Stdlib\DateTime;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add an alias for DateTime or use FQCN due to Magento 2.2.x support PHP 7.0 version and this code will fail with fatal error:

Cannot use Magento\Framework\Stdlib\DateTime as DateTime because the name is already in use in /opt/bamboo/builds/MPAF-SPERFEE-CEEC/build-775/lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php on line 14

This is a PHP core bug, that was fixed in 7.0.13

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for that. I wasn't aware of that bug. Is this 2.2.x specific, as the same commit was made to the 2.3-develop branch.


/**
* Timezone library
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Timezone implements TimezoneInterface
{
Expand All @@ -29,12 +36,12 @@ class Timezone implements TimezoneInterface
protected $_scopeType;

/**
* @var \Magento\Framework\App\ScopeResolverInterface
* @var ScopeResolverInterface
*/
protected $_scopeResolver;

/**
* @var \Magento\Framework\Stdlib\DateTime
* @var DateTime
*/
protected $_dateTime;

Expand All @@ -44,28 +51,28 @@ class Timezone implements TimezoneInterface
protected $_defaultTimezonePath;

/**
* @var \Magento\Framework\App\Config\ScopeConfigInterface
* @var ScopeConfigInterface
*/
protected $_scopeConfig;

/**
* @var \Magento\Framework\Locale\ResolverInterface
* @var ResolverInterface
*/
protected $_localeResolver;

/**
* @param \Magento\Framework\App\ScopeResolverInterface $scopeResolver
* @param \Magento\Framework\Locale\ResolverInterface $localeResolver
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param ScopeResolverInterface $scopeResolver
* @param ResolverInterface $localeResolver
* @param DateTime $dateTime
* @param ScopeConfigInterface $scopeConfig
* @param string $scopeType
* @param string $defaultTimezonePath
*/
public function __construct(
\Magento\Framework\App\ScopeResolverInterface $scopeResolver,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
ScopeResolverInterface $scopeResolver,
ResolverInterface $localeResolver,
DateTime $dateTime,
ScopeConfigInterface $scopeConfig,
$scopeType,
$defaultTimezonePath
) {
Expand Down Expand Up @@ -174,6 +181,8 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
$timeType,
new \DateTimeZone($timezone)
);

$date = $this->appendTimeIfNeeded($date, $includeTime);
$date = $formatter->parse($date) ?: (new \DateTime($date))->getTimestamp();
break;
}
Expand Down Expand Up @@ -226,7 +235,7 @@ public function scopeTimeStamp($scope = null)
*/
public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
{
if (!$scope instanceof \Magento\Framework\App\ScopeInterface) {
if (!$scope instanceof ScopeInterface) {
$scope = $this->_scopeResolver->getScope($scope);
}

Expand Down Expand Up @@ -318,4 +327,19 @@ public function convertConfigTimeToUtc($date, $format = 'Y-m-d H:i:s')

return $date->format($format);
}

/**
* Retrieve date with time
*
* @param string $date
* @param bool $includeTime
* @return string
*/
private function appendTimeIfNeeded($date, $includeTime)
{
if ($includeTime && !preg_match('/\d{1}:\d{2}/', $date)) {
$date .= " 0:00am";
}
return $date;
}
}