Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 73d5d0e

Browse files
authored
Remove text suffix in XLSX date formats (#341)
Some date formats have a text suffix, e.g. "mm/dd/yy;@". We should remove the ";...@" part.
1 parent 687c321 commit 73d5d0e

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/Spout/Reader/XLSX/Helper/DateFormatHelper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class DateFormatHelper
6262
public static function toPHPDateFormat($excelDateFormat)
6363
{
6464
// Remove brackets potentially present at the beginning of the format string
65-
$dateFormat = preg_replace('/^(\[\$[^\]]+?\])/i', '', $excelDateFormat);
65+
// and text portion of the format at the end of it (starting with ";")
66+
// See §18.8.31 of ECMA-376 for more detail.
67+
$dateFormat = preg_replace('/^(?:\[\$[^\]]+?\])?([^;]*).*/', '$1', $excelDateFormat);
6668

6769
// Double quotes are used to escape characters that must not be interpreted.
6870
// For instance, ["Day " dd] should result in "Day 13" and we should not try to interpret "D", "a", "y"

tests/Spout/Reader/XLSX/Helper/DateFormatHelperTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function dataProviderForTestToPHPDateFormat()
2929
['[$USD-F480]hh:mm AM/PM', 'h:i A'],
3030
['"Day " d', '\\D\\a\\y\\ j'],
3131
['yy "Year" m "Month"', 'y \\Y\\e\\a\\r n \\M\\o\\n\\t\\h'],
32+
['mmm-yy;@', 'M-y'],
33+
['[$-409]hh:mm AM/PM;"foo"@', 'h:i A'],
3234
];
3335
}
3436

0 commit comments

Comments
 (0)