Merge pull request #15424 from owncloud/failing-master-tests

Fix failing tests related to timezones on master
This commit is contained in:
Thomas Müller 2015-04-07 11:13:05 +02:00
commit 1f8e6e78cc
4 changed files with 28 additions and 16 deletions

View File

@ -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',

View File

@ -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;
}

View File

@ -25,7 +25,8 @@ namespace OCP;
interface IDateTimeZone {
/**
* @param bool|int $timestamp
* @return \DateTimeZone
*/
public function getTimeZone();
public function getTimeZone($timestamp = false);
}

View File

@ -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);