5dd7e883c6
* 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.
89 lines
2.8 KiB
PHP
89 lines
2.8 KiB
PHP
<?php
|
|
|
|
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 = [
|
|
'Europe/Prague',
|
|
'Asia/Tokyo',
|
|
'America/Indiana/Indianapolis',
|
|
'Pacific/Honolulu',
|
|
'Atlantic/St_Helena',
|
|
];
|
|
|
|
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 = '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);
|
|
}
|
|
}
|