Skip to content

Commit 96cc629

Browse files
authored
ENGCOM-7155: Refactor datetime class #26538
2 parents f6352f5 + 84e6108 commit 96cc629

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

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

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313
*/
1414
class DateTime
1515
{
16-
/**
17-
* Current config offset in seconds
18-
*
19-
* @var int
20-
*/
21-
private $_offset = 0;
22-
2316
/**
2417
* @var TimezoneInterface
2518
*/
@@ -31,7 +24,6 @@ class DateTime
3124
public function __construct(TimezoneInterface $localeDate)
3225
{
3326
$this->_localeDate = $localeDate;
34-
$this->_offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
3527
}
3628

3729
/**
@@ -78,8 +70,7 @@ public function gmtDate($format = null, $input = null)
7870
}
7971

8072
/**
81-
* Converts input date into date with timezone offset
82-
* Input date must be in GMT timezone
73+
* Converts input date into date with timezone offset. Input date must be in GMT timezone.
8374
*
8475
* @param string $format
8576
* @param int|string $input date in GMT timezone
@@ -122,8 +113,7 @@ public function gmtTimestamp($input = null)
122113
}
123114

124115
/**
125-
* Converts input date into timestamp with timezone offset
126-
* Input date must be in GMT timezone
116+
* Converts input date into timestamp with timezone offset. Input date must be in GMT timezone.
127117
*
128118
* @param int|string $input date in GMT timezone
129119
* @return int
@@ -157,18 +147,18 @@ public function timestamp($input = null)
157147
*/
158148
public function getGmtOffset($type = 'seconds')
159149
{
160-
$result = $this->_offset;
150+
$offset = $this->calculateOffset($this->_localeDate->getConfigTimezone());
161151
switch ($type) {
162152
case 'seconds':
163153
default:
164154
break;
165155
case 'minutes':
166-
$result = $result / 60;
156+
$offset = $offset / 60;
167157
break;
168158
case 'hours':
169-
$result = $result / 60 / 60;
159+
$offset = $offset / 60 / 60;
170160
break;
171161
}
172-
return $result;
162+
return $offset;
173163
}
174164
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public function testTimestamp($input)
4646
$this->assertEquals($expected, (new DateTime($timezone))->timestamp($input));
4747
}
4848

49+
public function testGtmOffset()
50+
{
51+
/** @var TimezoneInterface|\PHPUnit_Framework_MockObject_MockObject $timezone */
52+
$timezone = $this->getMockBuilder(TimezoneInterface::class)->getMock();
53+
$timezone->method('getConfigTimezone')->willReturn('Europe/Amsterdam');
54+
55+
/** @var DateTime|\PHPUnit_Framework_MockObject_MockObject $dateTime */
56+
$dateTime = $this->getMockBuilder(DateTime::class)
57+
->setConstructorArgs([$timezone])
58+
->setMethods(null)
59+
->getMock();
60+
61+
$this->assertEquals(3600, $dateTime->getGmtOffset());
62+
}
63+
4964
/**
5065
* @return array
5166
*/

0 commit comments

Comments
 (0)