mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-19 02:18:56 +00:00
04e7c30758
I ran the test suite using 32-bit PHP. There were 2 places where changes were needed due to 32-bit timestamps. Reader\\Xml.php was using strtotime as an intermediate step in converting a string timestamp to an Excel timestamp. The XML file type stores pure timestamps (i.e. no date portion) as, e.g., 1899-12-31T02:30:00.000, and that value causes an error using strtotime on a 32-bit system. However, it is sufficient to use that value in a DateTime constructor, and that will work for 32- and 64-bit. There was no test for that particular cell, so I added one to the XML read test. And that's when I discovered the getFormattedValue bug. The cell's format is `hh":"mm":"ss`. The quotes around the colons are disrupting the formatting. PhpSpreadsheet formats the cell by converting the Excel format to a Php Date format, in this case `H\:m\:s`. That's a problem, since Excel thinks 'm' means *minutes*, but PHP thinks it means *months*. This is not a problem when the colon is not quoted; there are ample tests for that. I added my best guess as to how to recognize this situation, changing `\:m` to `:i`. The XML read test now succeeds, and no other tests were broken by this change. Test Shared\\DateTest had one test where the expected result of converting to a Unix timestamp exceeds 2**32. Since a Unix timestamp is strictly an int, that test fails on a 32-bit system. In the discussion regarding recently merged PR #1870, it was felt that the user base might still be using the functions that convert to and from a timestamp. So, we should not drop this test, but, since it cannot succeed on a 32-bit system, I changed it to be skipped whenever the expected result exceeded PHP_INT_MAX. There are 3 "toTimestamp" functions within that test. Only one of these had been affected, but I thought it was a good idea to add additional tests to the others to demonstrate this condition. In the course of testing, I also discovered some 32-bit problems with bitwise and base-conversion functions. I am preparing separate PRs to deal with those.
235 lines
7.4 KiB
PHP
235 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Shared\Date;
|
|
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class DateTest extends TestCase
|
|
{
|
|
private $excelCalendar;
|
|
|
|
private $dttimezone;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->dttimezone = Date::getDefaultTimeZone();
|
|
$this->excelCalendar = Date::getExcelCalendar();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Date::setDefaultTimeZone($this->dttimezone);
|
|
Date::setExcelCalendar($this->excelCalendar);
|
|
}
|
|
|
|
public function testSetExcelCalendar(): void
|
|
{
|
|
$calendarValues = [
|
|
Date::CALENDAR_MAC_1904,
|
|
Date::CALENDAR_WINDOWS_1900,
|
|
];
|
|
|
|
foreach ($calendarValues as $calendarValue) {
|
|
$result = Date::setExcelCalendar($calendarValue);
|
|
self::assertTrue($result);
|
|
}
|
|
}
|
|
|
|
public function testSetExcelCalendarWithInvalidValue(): void
|
|
{
|
|
$unsupportedCalendar = '2012';
|
|
$result = Date::setExcelCalendar($unsupportedCalendar);
|
|
self::assertFalse($result);
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1900($expectedResult, ...$args): void
|
|
{
|
|
if (is_numeric($expectedResult) && ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN)) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::excelToTimestamp(...$args);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerDateTimeExcelToTimestamp1900()
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeTimestampToExcel1900
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeTimestampToExcel1900($expectedResult, ...$args): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::timestampToExcel(...$args);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public function providerDateTimeTimestampToExcel1900()
|
|
{
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeDateTimeToExcel
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeDateTimeToExcel($expectedResult, ...$args): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::dateTimeToExcel(...$args);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public function providerDateTimeDateTimeToExcel()
|
|
{
|
|
return require 'tests/data/Shared/Date/DateTimeToExcel.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeFormattedPHPToExcel1900
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeFormattedPHPToExcel1900($expectedResult, ...$args): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::formattedPHPToExcel(...$args);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public function providerDateTimeFormattedPHPToExcel1900()
|
|
{
|
|
return require 'tests/data/Shared/Date/FormattedPHPToExcel1900.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1904
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1904($expectedResult, ...$args): void
|
|
{
|
|
if (is_numeric($expectedResult) && ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN)) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
|
|
|
$result = Date::excelToTimestamp(...$args);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerDateTimeExcelToTimestamp1904()
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1904.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeTimestampToExcel1904
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeTimestampToExcel1904($expectedResult, ...$args): void
|
|
{
|
|
Date::setExcelCalendar(Date::CALENDAR_MAC_1904);
|
|
|
|
$result = Date::timestampToExcel(...$args);
|
|
self::assertEqualsWithDelta($expectedResult, $result, 1E-5);
|
|
}
|
|
|
|
public function providerDateTimeTimestampToExcel1904()
|
|
{
|
|
return require 'tests/data/Shared/Date/TimestampToExcel1904.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerIsDateTimeFormatCode
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testIsDateTimeFormatCode($expectedResult, ...$args): void
|
|
{
|
|
$result = Date::isDateTimeFormatCode(...$args);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerIsDateTimeFormatCode()
|
|
{
|
|
return require 'tests/data/Shared/Date/FormatCodes.php';
|
|
}
|
|
|
|
/**
|
|
* @dataProvider providerDateTimeExcelToTimestamp1900Timezone
|
|
*
|
|
* @param mixed $expectedResult
|
|
*/
|
|
public function testDateTimeExcelToTimestamp1900Timezone($expectedResult, ...$args): void
|
|
{
|
|
if (is_numeric($expectedResult) && ($expectedResult > PHP_INT_MAX || $expectedResult < PHP_INT_MIN)) {
|
|
self::markTestSkipped('Test invalid on 32-bit system.');
|
|
}
|
|
Date::setExcelCalendar(Date::CALENDAR_WINDOWS_1900);
|
|
|
|
$result = Date::excelToTimestamp(...$args);
|
|
self::assertEquals($expectedResult, $result);
|
|
}
|
|
|
|
public function providerDateTimeExcelToTimestamp1900Timezone()
|
|
{
|
|
return require 'tests/data/Shared/Date/ExcelToTimestamp1900Timezone.php';
|
|
}
|
|
|
|
public function testVarious(): void
|
|
{
|
|
Date::setDefaultTimeZone('UTC');
|
|
self::assertFalse(Date::stringToExcel('2019-02-29'));
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28'));
|
|
self::assertTrue((bool) Date::stringToExcel('2019-02-28 11:18'));
|
|
self::assertFalse(Date::stringToExcel('2019-02-28 11:71'));
|
|
$date = Date::PHPToExcel('2020-01-01');
|
|
self::assertEquals(43831.0, $date);
|
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
$sheet->setCellValue('B1', 'x');
|
|
$val = $sheet->getCell('B1')->getValue();
|
|
self::assertFalse(Date::timestampToExcel($val));
|
|
$cell = $sheet->getCell('A1');
|
|
self::assertNotNull($cell);
|
|
$cell->setValue($date);
|
|
$sheet->getStyle('A1')
|
|
->getNumberFormat()
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
self::assertTrue(null !== $cell && Date::isDateTime($cell));
|
|
$cella2 = $sheet->getCell('A2');
|
|
self::assertNotNull($cella2);
|
|
$cella2->setValue('=A1+2');
|
|
$sheet->getStyle('A2')
|
|
->getNumberFormat()
|
|
->setFormatCode(NumberFormat::FORMAT_DATE_DATETIME);
|
|
self::assertTrue(null !== $cella2 && Date::isDateTime($cella2));
|
|
$cella3 = $sheet->getCell('A3');
|
|
self::assertNotNull($cella3);
|
|
$cella3->setValue('=A1+4');
|
|
$sheet->getStyle('A3')
|
|
->getNumberFormat()
|
|
->setFormatCode('0.00E+00');
|
|
self::assertFalse(null !== $cella3 && Date::isDateTime($cella3));
|
|
}
|
|
}
|