Skip to content

fix rounding issues with microseconds calculation #4476

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

Closed
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
28 changes: 21 additions & 7 deletions src/PhpSpreadsheet/Shared/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,13 +223,27 @@ public static function excelToDateTimeObject(float|int $excelTimestamp, null|Dat
}
$days = floor($excelTimestamp);
$partDay = $excelTimestamp - $days;
$hms = 86400 * $partDay;
$microseconds = (int) round(fmod($hms, 1) * 1000000);
$hms = (int) floor($hms);
$hours = intdiv($hms, 3600);
$hms -= $hours * 3600;
$minutes = intdiv($hms, 60);
$seconds = $hms % 60;
$hoursInMs = 86400 * $partDay;
$wholeSeconds = (int) floor($hoursInMs);

// flooring here might lose data due to precision issues, hence round it
$microseconds = (int) round(($hoursInMs - $wholeSeconds) * 1_000_000);
$microseconds = (int) round($microseconds, -2);

// rounding may lead to edge cases
if ($microseconds === 1_000_000) {
$microseconds = 0;
++$wholeSeconds;
}
if ($wholeSeconds >= 86400) {
$wholeSeconds = 0;
++$days;
}

$hours = intdiv($wholeSeconds, 3600);
$remainingSeconds = $wholeSeconds % 3600;
$minutes = intdiv($remainingSeconds, 60);
$seconds = $remainingSeconds % 60;

if ($days >= 0) {
$days = '+' . $days;
Expand Down
49 changes: 48 additions & 1 deletion tests/PhpSpreadsheetTests/Reader/Xlsx/ExplicitDateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;

use DateTime;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class ExplicitDateTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -20,7 +24,7 @@
self::fail('Unable to read file');
} else {
// confirm that file contains type "d" cells
self::assertStringContainsString('<c r="A3" s="1" t="d"><v>2021-12-31T23:44:51.894</v></c>', $data);

Check failure on line 27 in tests/PhpSpreadsheetTests/Reader/Xlsx/ExplicitDateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

Failed asserting that '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n

Check failure on line 27 in tests/PhpSpreadsheetTests/Reader/Xlsx/ExplicitDateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.4

Failed asserting that '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n

Check failure on line 27 in tests/PhpSpreadsheetTests/Reader/Xlsx/ExplicitDateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

Failed asserting that '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n

Check failure on line 27 in tests/PhpSpreadsheetTests/Reader/Xlsx/ExplicitDateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

Failed asserting that '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n
self::assertStringContainsString('<c r="B3" s="2" t="d"><v>2021-12-31</v></c>', $data);
self::assertStringContainsString('<c r="C3" s="3" t="d"><v>23:44:51.894</v></c>', $data);
}
Expand All @@ -40,12 +44,55 @@
$formatted = $sheet->getCell('B3')->getFormattedValue();
self::assertEquals(44561, $value);
self::assertSame('2021-12-31', $formatted);
// Time only
// Time only, with seconds
$value = $sheet->getCell('C3')->getValue();
$formatted = $sheet->getCell('C3')->getFormattedValue();
self::assertEqualsWithDelta(0.98948, $value, 0.00001);
self::assertSame('23:44:52', $formatted);
// Time only, full minute
$value = $sheet->getCell('F3')->getValue();
$formatted = $sheet->getCell('F3')->getFormattedValue();
self::assertEqualsWithDelta(0.5673611, $value, 0.00001);
self::assertSame('13:37', $formatted);

$spreadsheet->disconnectWorksheets();
}

public function testThatDateTimesCanBePersistedAndReread(): void
{
$originalDateTime = new DateTime('2020-10-21T14:55:31');

$dateTimeFromSpreadsheet = $this->getDateTimeFrom($this->excelSheetWithDateTime($originalDateTime));
$dateTimeFromSpreadsheetAfterPersistAndReread = $this->getDateTimeFrom($this->persistAndReread($this->excelSheetWithDateTime($originalDateTime)));

self::assertEquals($originalDateTime, $dateTimeFromSpreadsheet);
self::assertEquals($originalDateTime, $dateTimeFromSpreadsheetAfterPersistAndReread);
}

private function excelSheetWithDateTime(DateTime $dateTime): Spreadsheet
{
$spreadsheet = new Spreadsheet();
$spreadsheet->getActiveSheet()->setCellValue('A1', Date::dateTimeToExcel($dateTime));

return $spreadsheet;
}

public function getDateTimeFrom(Spreadsheet $spreadsheet): DateTime
{
$value = $spreadsheet->getSheet(0)->getCell('A1')->getCalculatedValue();
self::assertIsNumeric($value);
$value = (float) $value;

return Date::excelToDateTimeObject($value);
}

private function persistAndReread(Spreadsheet $spreadsheet): Spreadsheet
{
$tempPointer = tmpfile();
$tempFileName = stream_get_meta_data($tempPointer)['uri'] ?? null;
self::assertNotNull($tempFileName, 'Temp file not created');
(new Xlsx($spreadsheet))->save($tempFileName);

return (new \PhpOffice\PhpSpreadsheet\Reader\Xlsx())->load($tempFileName);
}
}
Binary file modified tests/data/Reader/XLSX/explicitdate.xlsx
Binary file not shown.
Loading