diff --git a/CHANGELOG.md b/CHANGELOG.md index d006720..4abf117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Fixed +- ExcelBook::packDateValues() rejects calendar years before 1900 and calendar serials below 1, matching packDate() boundaries while preserving explicit all-zero date fields for time-only values. Other LibXL calendar normalization remains unchanged. - ExcelBook::packDate() accepts supported pre-epoch timestamps instead of rejecting every negative timestamp. packDate() and AS_DATE writes reject local dates before 1900-01-01 (1900 system) or 1904-01-02 (1904 system), whose serials are unsupported, collide, or are reserved for time-only values. - Reject unsupported data-validation types, operators, and error styles with a warning and false instead of silently omitting the corresponding XLSX attributes. - Reject unsupported custom-filter operators with a warning and false before LibXL writes empty XLSX operator attributes. All six documented operators and the optional second-criterion sentinel remain accepted. diff --git a/docs/ExcelBook.php b/docs/ExcelBook.php index 6e50d4e..f7f1426 100644 --- a/docs/ExcelBook.php +++ b/docs/ExcelBook.php @@ -377,6 +377,11 @@ public function packDate(int $timestamp): float|false /** * Pack a date from single values into an Excel double * + * Calendar dates before 1900 or producing serials below 1 return false, + * matching packDate() boundaries. All-zero year/month/day fields explicitly + * produce time-only values. Other calendar normalization remains LibXL-defined, + * including Excel's fictitious 1900-02-29 in the 1900 date system. + * * @param int $year * @param int $month * @param int $day diff --git a/excel.c b/excel.c index f2df4da..0906631 100644 --- a/excel.c +++ b/excel.c @@ -3565,7 +3565,7 @@ EXCEL_METHOD(Book, packDateValues) // all-zero date fields generate a time-only value if (year != 0 || month != 0 || day != 0) { - if (year < 1 || year > INT_MAX) { + if (year < 1900 || year > INT_MAX) { php_error_docref(NULL, E_WARNING, "Invalid '" ZEND_LONG_FMT "' value for year", year); RETURN_FALSE; } @@ -3581,7 +3581,10 @@ EXCEL_METHOD(Book, packDateValues) BOOK_FROM_OBJECT(book, object); - if ((dt = xlBookDatePack(book, year, month, day, hour, min, sec, 0)) == -1) { + dt = xlBookDatePack(book, year, month, day, hour, min, sec, 0); + /* Calendar serials below 1 are negative or reserved for time-only values. + * Keep explicit all-zero date fields as LibXL's time-only operation. */ + if (dt == -1 || ((year != 0 || month != 0 || day != 0) && dt < 1)) { RETURN_FALSE; } RETURN_DOUBLE(dt); diff --git a/tests/280_packdatevalues_calendar_boundaries.phpt b/tests/280_packdatevalues_calendar_boundaries.phpt new file mode 100644 index 0000000..2eab35d --- /dev/null +++ b/tests/280_packdatevalues_calendar_boundaries.phpt @@ -0,0 +1,80 @@ +--TEST-- +Component date packing rejects unsupported calendar boundaries while preserving explicit times +--EXTENSIONS-- +excel +--INI-- +date.timezone=UTC +--FILE-- +packDateValues(...$parts); + } finally { + restore_error_handler(); + } +} +foreach ([false, true] as $xlsx) { + foreach ([false, true] as $date1904) { + $book = new ExcelBook(null, null, $xlsx); + $book->setDate1904($date1904); + $first = $date1904 ? [1904, 1, 2] : [1900, 1, 1]; + foreach ([0, 12, 23] as $hour) { + $parts = [...$first, $hour, 0, 0]; + $serial = $book->packDateValues(...$parts); + check($serial === 1.0 + $hour / 24, 'first supported calendar serial'); + // Dates near 1900 exceed the 32-bit PHP timestamp range; component + // serial checks and every rejection below still run on 32-bit PHP. + if (PHP_INT_SIZE >= 8) { + $timestamp = strtotime(sprintf('%04d-%02d-%02d %02d:00:00', ...[...$first, $hour])); + check($book->unpackDate($serial) === $timestamp, 'calendar round trip'); + check($book->packDate($timestamp) === $serial, 'timestamp/component agreement'); + } + } + $invalidDates = [[1, 1, 1], [1800, 1, 1], [1899, 12, 30], [1899, 12, 31]]; + if ($date1904) { + $invalidDates = array_merge($invalidDates, [[1900, 1, 1], [1903, 12, 31], [1904, 1, 1]]); + } + foreach ($invalidDates as $date) { + foreach ([[0, 0, 0], [12, 0, 0], [23, 59, 59]] as $time) { + check(packWithoutWarnings($book, [...$date, ...$time], $warnings) === false, + 'unsupported calendar date rejected'); + } + } + foreach ([[0, 0, 0], [12, 0, 0], [23, 59, 59]] as $time) { + $seconds = $time[0] * 3600 + $time[1] * 60 + $time[2]; + $serial = $book->packDateValues(0, 0, 0, ...$time); + check(abs($serial - $seconds / 86400) < 1e-12, 'explicit time-only serial'); + check($book->unpackDate($serial) === $seconds, 'time-only round trip'); + } + // Invalid time validation still precedes calendar-year validation. + foreach ([[24, 0, 0], [0, 60, 0], [0, 0, 60]] as $time) { + check(packWithoutWarnings($book, [2024, 1, 1, ...$time], $validYearWarnings) === false, + 'invalid time rejected'); + check(packWithoutWarnings($book, [1899, 1, 1, ...$time], $oldYearWarnings) === false, + 'invalid time with old year rejected'); + check(count($validYearWarnings) === 1 && $validYearWarnings === $oldYearWarnings, + 'time validation precedence unchanged'); + } + // This change does not replace LibXL calendar normalization. + check($book->packDateValues(2024, 2, 30, 12, 0, 0) === + $book->packDateValues(2024, 3, 1, 12, 0, 0), 'month-day normalization preserved'); + if (!$date1904) { + check($book->packDateValues(1900, 2, 29, 12, 0, 0) === 60.5, 'Excel leap-day quirk preserved'); + } + echo ($xlsx ? 'xlsx' : 'xls'), ' ', ($date1904 ? '1904' : '1900'), ": OK\n"; + } +} +?> +--EXPECT-- +xls 1900: OK +xls 1904: OK +xlsx 1900: OK +xlsx 1904: OK