Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Two 32-bit Timestamp Problems, and Minor getFormattedValue Bug #1891

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/PhpSpreadsheet/Reader/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Reader;

use DateTime;
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Cell\AddressHelper;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Cell\DataType;
Expand Down Expand Up @@ -557,7 +559,8 @@ public function loadIntoExisting($pFilename, Spreadsheet $spreadsheet)
break;
case 'DateTime':
$type = DataType::TYPE_NUMERIC;
$cellValue = Date::PHPToExcel(strtotime($cellValue . ' UTC'));
$dateTime = new DateTime($cellValue, new DateTimeZone('UTC'));
$cellValue = Date::PHPToExcel($dateTime);

break;
case 'Error':
Expand Down
4 changes: 4 additions & 0 deletions src/PhpSpreadsheet/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,10 @@ private static function formatAsDate(&$value, &$format): void
$format = preg_replace_callback('/"(.*)"/U', ['self', 'escapeQuotesCallback'], $format);

$dateObj = Date::excelToDateTimeObject($value);
// If the colon preceding minute had been quoted, as happens in
// Excel 2003 XML formats, m will not have been changed to i above.
// Change it now.
MarkBaker marked this conversation as resolved.
Show resolved Hide resolved
$format = \preg_replace('/\\\\:m/', ':i', $format);
$value = $dateObj->format($format);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/PhpSpreadsheetTests/Reader/Xml/XmlLoadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public function testLoad(): void
self::assertEquals('# ?0/??0', $sheet->getCell('A11')->getStyle()->getNumberFormat()->getFormatCode());
// Same pattern, same value, different display in Gnumeric vs Excel
//self::assertEquals('1 1/2', $sheet->getCell('A11')->getFormattedValue());
self::assertEquals('hh":"mm":"ss', $sheet->getCell('A13')->getStyle()->getNumberFormat()->getFormatCode());
self::assertEquals('02:30:00', $sheet->getCell('A13')->getFormattedValue());
self::assertEquals('d/m/yy hh":"mm', $sheet->getCell('A15')->getStyle()->getNumberFormat()->getFormatCode());
self::assertEquals('19/12/60 01:30', $sheet->getCell('A15')->getFormattedValue());

self::assertEquals('=B1+C1', $sheet->getCell('H1')->getValue());
self::assertEquals('=E2&F2', $sheet->getCell('J2')->getValue());
Expand Down
13 changes: 13 additions & 0 deletions tests/PhpSpreadsheetTests/Shared/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

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
Expand Down Expand Up @@ -47,6 +51,9 @@ public function testSetExcelCalendarWithInvalidValue(): void
*/
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);
Expand Down Expand Up @@ -119,6 +126,9 @@ public function providerDateTimeFormattedPHPToExcel1900()
*/
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);
Expand Down Expand Up @@ -171,6 +181,9 @@ public function providerIsDateTimeFormatCode()
*/
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);
Expand Down
7 changes: 7 additions & 0 deletions tests/data/Shared/Date/ExcelToTimestamp1900.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,11 @@
10666,
0.12345,
],
// 29-Apr-2038 00:00:00 beyond PHP 32-bit Latest Date
[
2156112000,
50524,
],
[-2147483648, -2147483648 / 86400], // Okay on 64- and 32-bit systems
[-2147483649, -2147483649 / 86400], // Skipped test on 32-bit
];
5 changes: 5 additions & 0 deletions tests/data/Shared/Date/ExcelToTimestamp1904.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@
gmmktime(13, 02, 13, 1, 1, 1904), // 32-bit safe - no Y2038 problem
0.54321,
],
// 29-Apr-2038 00:00:00 beyond PHP 32-bit Latest Date
[
2156112000,
49062,
],
];