Skip to content

Commit ad2272d

Browse files
ENGCOM-5018: Set timezone on DateTime object not in constructor #22463
2 parents 66b05b3 + fa52657 commit ad2272d

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/internal/Magento/Framework/Stdlib/DateTime/Timezone.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public function date($date = null, $locale = null, $useTimezone = true, $include
196196
public function scopeDate($scope = null, $date = null, $includeTime = false)
197197
{
198198
$timezone = $this->_scopeConfig->getValue($this->getDefaultTimezonePath(), $this->_scopeType, $scope);
199-
$date = new \DateTime(is_numeric($date) ? '@' . $date : $date, new \DateTimeZone($timezone));
199+
$date = new \DateTime(is_numeric($date) ? '@' . $date : $date);
200+
$date->setTimezone(new \DateTimeZone($timezone));
200201
if (!$includeTime) {
201202
$date->setTime(0, 0, 0);
202203
}
@@ -247,13 +248,8 @@ public function isScopeDateInInterval($scope, $dateFrom = null, $dateTo = null)
247248
$toTimeStamp += 86400;
248249
}
249250

250-
$result = false;
251-
if (!$this->_dateTime->isEmptyDate($dateFrom) && $scopeTimeStamp < $fromTimeStamp) {
252-
} elseif (!$this->_dateTime->isEmptyDate($dateTo) && $scopeTimeStamp > $toTimeStamp) {
253-
} else {
254-
$result = true;
255-
}
256-
return $result;
251+
return !(!$this->_dateTime->isEmptyDate($dateFrom) && $scopeTimeStamp < $fromTimeStamp ||
252+
!$this->_dateTime->isEmptyDate($dateTo) && $scopeTimeStamp > $toTimeStamp);
257253
}
258254

259255
/**

lib/internal/Magento/Framework/Stdlib/Test/Unit/DateTime/TimezoneTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,15 @@ private function scopeConfigWillReturnConfiguredTimezone($configuredTimezone)
253253
{
254254
$this->scopeConfig->method('getValue')->with('', '', null)->willReturn($configuredTimezone);
255255
}
256+
257+
public function testCheckIfScopeDateSetsTimeZone()
258+
{
259+
$scopeDate = new \DateTime('now', new \DateTimeZone('America/Vancouver'));
260+
$this->scopeConfig->method('getValue')->willReturn('America/Vancouver');
261+
262+
$this->assertEquals(
263+
$scopeDate->getTimezone(),
264+
$this->getTimezone()->scopeDate(0, $scopeDate->getTimestamp())->getTimezone()
265+
);
266+
}
256267
}

0 commit comments

Comments
 (0)