Fix Issue 1441 (isDateTime and Formulas) (#1480)

* Fix Issue 1441 (isDateTime and Formulas)

When you have a date-field which is a formula, isDateTime returns false.
https://github.com/PHPOffice/PhpSpreadsheet/issues/1441

Report makes sense; fixed as suggested. Also fixed a few minor
related issues, and added tests so that Shared/Date and Shared/TimeZone
are now completely covered.

Date/setDefaultTimeZone and TimeZone/setTimeZone were not consistent
about what to do in event of failure - return false or throw.
They will now both return false, which is what Date's function
said it would do in its doc block anyhow. Date/validateTimeZone will
continue to throw; it was protected, but was never called outside
Date, so I changed it to private.

TimeZone/getTimeZoneAdjustment checked for 'UST' when it probably
meant 'UTC', and, as it turns out, the check is not even needed.

The most serious problem was that TimeZone/validateTimeZone does not
check the backwards-compatible time zones. The timezone project
aggressively, and very controversially, "demotes" timezones;
such timezones eventually wind up in the PHP backwards-compatible list.
We want to make sure to check that list so that our applications do not
break when this happens.
This commit is contained in:
oleibman
2020-05-24 11:02:39 -07:00
committed by GitHub
parent 585409a949
commit 5dd7e883c6
5 changed files with 125 additions and 18 deletions
@@ -2,11 +2,29 @@
namespace PhpOffice\PhpSpreadsheetTests\Shared;
use DateTime;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\TimeZone;
use PHPUnit\Framework\TestCase;
class TimeZoneTest extends TestCase
{
private $tztimezone;
private $dttimezone;
protected function setUp(): void
{
$this->tztimezone = TimeZone::getTimeZone();
$this->dttimezone = Date::getDefaultTimeZone();
}
protected function tearDown(): void
{
TimeZone::setTimeZone($this->tztimezone);
Date::setDefaultTimeZone($this->dttimezone);
}
public function testSetTimezone(): void
{
$timezoneValues = [
@@ -20,13 +38,51 @@ class TimeZoneTest extends TestCase
foreach ($timezoneValues as $timezoneValue) {
$result = TimeZone::setTimezone($timezoneValue);
self::assertTrue($result);
$result = Date::setDefaultTimezone($timezoneValue);
self::assertTrue($result);
}
}
public function testSetTimezoneBackwardCompatible(): void
{
$bcTimezone = 'Etc/GMT+10';
$result = TimeZone::setTimezone($bcTimezone);
self::assertTrue($result);
$result = Date::setDefaultTimezone($bcTimezone);
self::assertTrue($result);
}
public function testSetTimezoneWithInvalidValue(): void
{
$unsupportedTimezone = 'Etc/GMT+10';
$unsupportedTimezone = 'XEtc/GMT+10';
$result = TimeZone::setTimezone($unsupportedTimezone);
self::assertFalse($result);
$result = Date::setDefaultTimezone($unsupportedTimezone);
self::assertFalse($result);
}
public function testTimeZoneAdjustmentsInvalidTz(): void
{
$this->expectException(\PhpOffice\PhpSpreadsheet\Exception::class);
$dtobj = DateTime::createFromFormat('Y-m-d H:i:s', '2008-09-22 00:00:00');
$tstmp = $dtobj->getTimestamp();
$unsupportedTimeZone = 'XEtc/GMT+10';
TimeZone::getTimeZoneAdjustment($unsupportedTimeZone, $tstmp);
}
public function testTimeZoneAdjustments(): void
{
$dtobj = DateTime::createFromFormat('Y-m-d H:i:s', '2008-01-01 00:00:00');
$tstmp = $dtobj->getTimestamp();
$supportedTimeZone = 'UTC';
$adj = TimeZone::getTimeZoneAdjustment($supportedTimeZone, $tstmp);
self::assertEquals(0, $adj);
$supportedTimeZone = 'America/Toronto';
$adj = TimeZone::getTimeZoneAdjustment($supportedTimeZone, $tstmp);
self::assertEquals(-18000, $adj);
$supportedTimeZone = 'America/Chicago';
TimeZone::setTimeZone($supportedTimeZone);
$adj = TimeZone::getTimeZoneAdjustment(null, $tstmp);
self::assertEquals(-21600, $adj);
}
}