diff --git a/apps/files/tests/controller/apicontrollertest.php b/apps/files/tests/controller/apicontrollertest.php index 5d32a693ad30f6e55a303c2a07655e02323db52d..bbab711310c387adccd37f64edf2e3e0db41a984 100644 --- a/apps/files/tests/controller/apicontrollertest.php +++ b/apps/files/tests/controller/apicontrollertest.php @@ -110,7 +110,7 @@ class ApiControllerTest extends TestCase { [ 'id' => null, 'parentId' => null, - 'date' => 'January 1, 1970 at 12:00:55 AM GMT+0', + 'date' => \OCP\Util::formatDate(55), 'mtime' => 55000, 'icon' => \OCA\Files\Helper::determineIcon($fileInfo), 'name' => 'root.txt', @@ -175,7 +175,7 @@ class ApiControllerTest extends TestCase { [ 'id' => null, 'parentId' => null, - 'date' => 'January 1, 1970 at 12:00:55 AM GMT+0', + 'date' => \OCP\Util::formatDate(55), 'mtime' => 55000, 'icon' => \OCA\Files\Helper::determineIcon($fileInfo1), 'name' => 'root.txt', @@ -194,7 +194,7 @@ class ApiControllerTest extends TestCase { [ 'id' => null, 'parentId' => null, - 'date' => 'January 1, 1970 at 12:16:39 AM GMT+0', + 'date' => \OCP\Util::formatDate(999), 'mtime' => 999000, 'icon' => \OCA\Files\Helper::determineIcon($fileInfo2), 'name' => 'root.txt', diff --git a/lib/private/datetimezone.php b/lib/private/datetimezone.php index acc956e66f363c2e2c7c3b00e2a47b787ebd11f9..512c39be3ea49503f4ef3ff32b56f7f07ede22a6 100644 --- a/lib/private/datetimezone.php +++ b/lib/private/datetimezone.php @@ -48,13 +48,14 @@ class DateTimeZone implements IDateTimeZone { /** * Get the timezone of the current user, based on his session information and config data * + * @param bool|int $timestamp * @return \DateTimeZone */ - public function getTimeZone() { + public function getTimeZone($timestamp = false) { $timeZone = $this->config->getUserValue($this->session->get('user_id'), 'core', 'timezone', null); if ($timeZone === null) { if ($this->session->exists('timezone')) { - return $this->guessTimeZoneFromOffset($this->session->get('timezone')); + return $this->guessTimeZoneFromOffset($this->session->get('timezone'), $timestamp); } $timeZone = $this->getDefaultTimeZone(); } @@ -74,9 +75,10 @@ class DateTimeZone implements IDateTimeZone { * we try to find it manually, before falling back to UTC. * * @param mixed $offset + * @param bool|int $timestamp * @return \DateTimeZone */ - protected function guessTimeZoneFromOffset($offset) { + protected function guessTimeZoneFromOffset($offset, $timestamp) { try { // Note: the timeZone name is the inverse to the offset, // so a positive offset means negative timeZone @@ -93,7 +95,13 @@ class DateTimeZone implements IDateTimeZone { // we try to guess one timezone that has the same offset foreach (\DateTimeZone::listIdentifiers() as $timeZone) { $dtz = new \DateTimeZone($timeZone); - $dtOffset = $dtz->getOffset(new \DateTime()); + $dateTime = new \DateTime(); + + if ($timestamp !== false) { + $dateTime->setTimestamp($timestamp); + } + + $dtOffset = $dtz->getOffset($dateTime); if ($dtOffset == 3600 * $offset) { return $dtz; } diff --git a/lib/public/idatetimezone.php b/lib/public/idatetimezone.php index 30fbde15ac84cc520b374f0bed4e2b307acf5229..eb74074aa468eeefece3181d06440a1dd051c314 100644 --- a/lib/public/idatetimezone.php +++ b/lib/public/idatetimezone.php @@ -25,7 +25,8 @@ namespace OCP; interface IDateTimeZone { /** + * @param bool|int $timestamp * @return \DateTimeZone */ - public function getTimeZone(); + public function getTimeZone($timestamp = false); } diff --git a/tests/lib/util.php b/tests/lib/util.php index 49399c8cf0183a791b0a53dc021ebe39b6a40df9..e52a9fcc618f087e53b79229e558a310033428ea 100644 --- a/tests/lib/util.php +++ b/tests/lib/util.php @@ -54,24 +54,27 @@ class Test_Util extends \Test\TestCase { public function formatDateWithTZFromSessionData() { return array( - array(3, 'October 13, 2012 at 2:53:25 PM GMT+3'), - array(15, 'October 13, 2012 at 11:53:25 AM GMT+0'), - array(-13, 'October 13, 2012 at 11:53:25 AM GMT+0'), - array(9.5, 'October 13, 2012 at 9:23:25 PM GMT+9:30'), - array(-4.5, 'October 13, 2012 at 7:23:25 AM GMT-4:30'), - array(15.5, 'October 13, 2012 at 11:53:25 AM GMT+0'), + array(3, 'October 13, 2012 at 2:53:25 PM GMT+3', 'Etc/GMT-3'), + array(15, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'), + array(-13, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'), + array(9.5, 'October 13, 2012 at 9:23:25 PM GMT+9:30', 'Australia/Darwin'), + array(-4.5, 'October 13, 2012 at 7:23:25 AM GMT-4:30', 'America/Caracas'), + array(15.5, 'October 13, 2012 at 11:53:25 AM GMT+0', 'UTC'), ); } /** * @dataProvider formatDateWithTZFromSessionData */ - function testFormatDateWithTZFromSession($offset, $expected) { + function testFormatDateWithTZFromSession($offset, $expected, $expectedTimeZone) { date_default_timezone_set("UTC"); $oldDateTimeFormatter = \OC::$server->query('DateTimeFormatter'); \OC::$server->getSession()->set('timezone', $offset); - $newDateTimeFormatter = new \OC\DateTimeFormatter(\OC::$server->getDateTimeZone()->getTimeZone(), new \OC_L10N('lib', 'en')); + + $selectedTimeZone = \OC::$server->getDateTimeZone()->getTimeZone(1350129205); + $this->assertEquals($expectedTimeZone, $selectedTimeZone->getName()); + $newDateTimeFormatter = new \OC\DateTimeFormatter($selectedTimeZone, new \OC_L10N('lib', 'en')); $this->setDateFormatter($newDateTimeFormatter); $result = OC_Util::formatDate(1350129205, false);