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 all commits
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']
];
}
}
45 changes: 34 additions & 11 deletions lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
*/
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;

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

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

Expand All @@ -44,28 +50,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 ScopeResolverInterface $scopeResolver
* @param ResolverInterface $localeResolver
* @param \Magento\Framework\Stdlib\DateTime $dateTime
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param ScopeConfigInterface $scopeConfig
* @param string $scopeType
* @param string $defaultTimezonePath
*/
public function __construct(
\Magento\Framework\App\ScopeResolverInterface $scopeResolver,
\Magento\Framework\Locale\ResolverInterface $localeResolver,
ScopeResolverInterface $scopeResolver,
ResolverInterface $localeResolver,
\Magento\Framework\Stdlib\DateTime $dateTime,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
ScopeConfigInterface $scopeConfig,
$scopeType,
$defaultTimezonePath
) {
Expand Down Expand Up @@ -174,6 +180,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 +234,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 +326,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;
}
}