From ca506ba87f0cc06a127a25d694e6408c6acfe20b Mon Sep 17 00:00:00 2001 From: Christoph Ziegenberg Date: Sat, 20 Jun 2020 17:15:38 +0200 Subject: [PATCH] Corrected date time detection (#1492) * Corrected date time detection German and Swiss ZIP codes (special formats provided in German Excel versions) were detected as date time value, because the regular expression for date time formats falsely matched their formats ("\C\H\-00000" and "\D-00000"). --- src/PhpSpreadsheet/Shared/Date.php | 5 +++++ tests/data/Shared/Date/FormatCodes.php | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/src/PhpSpreadsheet/Shared/Date.php b/src/PhpSpreadsheet/Shared/Date.php index 9dc99292..cb37515d 100644 --- a/src/PhpSpreadsheet/Shared/Date.php +++ b/src/PhpSpreadsheet/Shared/Date.php @@ -387,6 +387,11 @@ class Date if ((substr($pFormatCode, 0, 1) == '_') || (substr($pFormatCode, 0, 2) == '0 ')) { return false; } + // Some "special formats" provided in German Excel versions were detected as date time value, + // so filter them out here - "\C\H\-00000" (Switzerland) and "\D-00000" (Germany). + if (\strpos($pFormatCode, '-00000') !== false) { + return false; + } // Try checking for any of the date formatting characters that don't appear within square braces if (preg_match('/(^|\])[^\[]*[' . self::$possibleDateFormatCharacters . ']/i', $pFormatCode)) { // We might also have a format mask containing quoted strings... diff --git a/tests/data/Shared/Date/FormatCodes.php b/tests/data/Shared/Date/FormatCodes.php index 64810de3..b558874c 100644 --- a/tests/data/Shared/Date/FormatCodes.php +++ b/tests/data/Shared/Date/FormatCodes.php @@ -150,4 +150,12 @@ return [ true, '"date " y-m-d', ], + [ + false, + '\C\H\-00000', + ], + [ + false, + '\D-00000', + ], ];