Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2174 from fvovan/issue-2174
Browse files Browse the repository at this point in the history
fix  getTimezone() error
  • Loading branch information
fvovan committed May 23, 2016
2 parents a429467 + 009cf7d commit 6858739
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
18 changes: 14 additions & 4 deletions library/CM/Http/Request/Abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,10 +525,20 @@ private function _getLanguageBrowser() {
private function _getTimeZoneFromCookie() {
if ($timeZoneOffset = $this->getCookie('timezoneOffset')) {
//timezoneOffset is seconds behind UTC
$timeZoneOffset = (int) $timeZoneOffset * -1;
$offsetHours = floor($timeZoneOffset / 3600);
$offsetMinutes = floor($timeZoneOffset % 3600 / 60);
return DateTime::createFromFormat('O', sprintf("%+03d%02d", $offsetHours, $offsetMinutes))->getTimezone();
$timeZoneOffset = (int) $timeZoneOffset;
if ($timeZoneOffset < -50400 || $timeZoneOffset > 43200) { //UTC+14 UTC-12
return null;
}
$timeZoneAbs = abs($timeZoneOffset);
$offsetHours = floor($timeZoneAbs / 3600);
$offsetMinutes = floor($timeZoneAbs % 3600 / 60);
if ($timeZoneOffset > 0) {
$offsetHours *= -1;
}
$dateTime = DateTime::createFromFormat('O', sprintf("%+03d%02d", $offsetHours, $offsetMinutes));
if (false !== $dateTime) {
return $dateTime->getTimezone();
}
}
return null;
}
Expand Down
21 changes: 17 additions & 4 deletions tests/library/CM/Http/Request/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,18 +268,31 @@ public function testSetSession() {
}

public function testGetTimeZoneFromCookie() {
$request = new CM_Http_Request_Get('/foo/bar/', ['cookie' => 'timezoneOffset=9000; clientId=7']);
$timeZone = $request->getTimeZone();
$this->assertInstanceOf('DateTimeZone', $timeZone);
$this->assertSame('-02:30', $timeZone->getName());

$request = new CM_Http_Request_Get('/foo/bar/', ['cookie' => 'timezoneOffset=-9000; clientId=7']);
$timeZone = $this->callProtectedMethod($request, 'getTimeZone');
$timeZone = $request->getTimeZone();
$this->assertInstanceOf('DateTimeZone', $timeZone);
$this->assertSame('+02:30', $timeZone->getName());

$request = new CM_Http_Request_Post('/foo/bar/', ['cookie' => 'timezoneOffset=3600']);
$timeZone = $this->callProtectedMethod($request, 'getTimeZone');
$timeZone = $request->getTimeZone();
$this->assertInstanceOf('DateTimeZone', $timeZone);
$this->assertSame('-01:00', $timeZone->getName());

$request = new CM_Http_Request_Post('/foo/bar/', ['cookie' => 'timezoneOffset=50400']);
$timeZone = $request->getTimeZone();
$this->assertNull($timeZone);

$request = new CM_Http_Request_Post('/foo/bar/', ['cookie' => 'timezoneOffset=-62400']);
$timeZone = $request->getTimeZone();
$this->assertNull($timeZone);

$request = new CM_Http_Request_Get('/foo/baz/');
$timeZone = $this->callProtectedMethod($request, 'getTimeZone');
$timeZone = $request->getTimeZone();
$this->assertNull($timeZone);
}

Expand Down

0 comments on commit 6858739

Please sign in to comment.