Skip to content

Commit

Permalink
Cell formats with escaped spaces were causing incorrect date formatting
Browse files Browse the repository at this point in the history
Fix issue where escaped spaces in cell format would cause incorrect
date format leading to incorrect time conversions

Fixes PHPOffice#557
Closes PHPOffice#558
  • Loading branch information
deekthesqueak authored and Frederic Delaunay committed Oct 29, 2018
1 parent 552e646 commit 4f2b624
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Cell formats with escaped spaces were causing incorrect date formatting - [#557](https://github.com/PHPOffice/PhpSpreadsheet/issues/557)

### Fixed

- Xlsx reader crashed when reading a file with workbook protection - [#553](https://github.com/PHPOffice/PhpSpreadsheet/pull/553)
Expand Down
2 changes: 1 addition & 1 deletion src/PhpSpreadsheet/Style/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ public static function toFormattedString($value, $format, $callBack = null)
}

// Convert any other escaped characters to quoted strings, e.g. (\T to "T")
$format = preg_replace('/(\\\(.))(?=(?:[^"]|"[^"]*")*$)/u', '"${2}"', $format);
$format = preg_replace('/(\\\([^ ]))(?=(?:[^"]|"[^"]*")*$)/u', '"${2}"', $format);

// Get the sections, there can be up to four sections, separated with a semi-colon (but only if not a quoted literal)
$sections = preg_split('/(;)(?=(?:[^"]|"[^"]*")*$)/u', $format);
Expand Down
10 changes: 10 additions & 0 deletions tests/data/Style/NumberFormatDates.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,14 @@
22269.0625,
'[DBNum1][$-804]m"月"d"日";@',
],
[
'07:35:00 AM',
43270.315972222,
'hh:mm:ss\ AM/PM',
],
[
'02:29:00 PM',
43270.603472222,
'hh:mm:ss\ AM/PM',
],
];

0 comments on commit 4f2b624

Please sign in to comment.