PhpSpreadsheet/tests/PhpSpreadsheetTests/Shared/TimeZoneTest.php

89 lines
2.8 KiB
PHP
Raw Normal View History

<?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);
}
2020-05-18 04:49:57 +00:00
public function testSetTimezone(): void
2015-05-17 13:00:02 +00:00
{
$timezoneValues = [
2015-05-17 13:00:02 +00:00
'Europe/Prague',
'Asia/Tokyo',
'America/Indiana/Indianapolis',
'Pacific/Honolulu',
'Atlantic/St_Helena',
];
2015-05-17 13:00:02 +00:00
foreach ($timezoneValues as $timezoneValue) {
$result = TimeZone::setTimezone($timezoneValue);
self::assertTrue($result);
$result = Date::setDefaultTimezone($timezoneValue);
self::assertTrue($result);
2015-05-17 13:00:02 +00:00
}
}
public function testSetTimezoneBackwardCompatible(): void
{
$bcTimezone = 'Etc/GMT+10';
$result = TimeZone::setTimezone($bcTimezone);
self::assertTrue($result);
$result = Date::setDefaultTimezone($bcTimezone);
self::assertTrue($result);
}
2020-05-18 04:49:57 +00:00
public function testSetTimezoneWithInvalidValue(): void
2015-05-17 13:00:02 +00:00
{
$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);
2015-05-17 13:00:02 +00:00
}
}