Skip to content

Commit 169a87a

Browse files
authored
Merge pull request #3379 from PHPOffice/Issue-3378_Improve-NumberFormat-Mask-Date-Identifier
Adjust identifier for Date Format Masking to prevent it picking out c…
2 parents 1f94687 + 5b105de commit 169a87a

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/PhpSpreadsheet/Style/NumberFormat.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public function getFormatCode()
217217
*
218218
* @return $this
219219
*/
220-
public function setFormatCode($formatCode)
220+
public function setFormatCode(string $formatCode)
221221
{
222222
if ($formatCode == '') {
223223
$formatCode = self::FORMAT_GENERAL;
@@ -251,11 +251,11 @@ public function getBuiltInFormatCode()
251251
/**
252252
* Set Built-In Format Code.
253253
*
254-
* @param int $formatCodeIndex
254+
* @param int $formatCodeIndex Id of the built-in format code to use
255255
*
256256
* @return $this
257257
*/
258-
public function setBuiltInFormatCode($formatCodeIndex)
258+
public function setBuiltInFormatCode(int $formatCodeIndex)
259259
{
260260
if ($this->isSupervisor) {
261261
$styleArray = $this->getStyleArray(['formatCode' => self::builtInFormatCode($formatCodeIndex)]);

src/PhpSpreadsheet/Style/NumberFormat/Formatter.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ function ($matches) {
170170
$format = (string) preg_replace('/_.?/ui', ' ', $format);
171171

172172
// Let's begin inspecting the format and converting the value to a formatted string
173-
174173
// Check for date/time characters (not inside quotes)
175-
if (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format, $matches)) {
174+
if (
175+
(preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format)) &&
176+
(preg_match('/0(?![^\[]*\])/miu', $format) === 0)
177+
) {
176178
// datetime format
177179
$value = DateFormatter::format($value, $format);
178180
} else {

tests/data/Style/NumberFormat.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,4 +1464,19 @@
14641464
'truncate to 1 decimal' => ['009 7.1', 97.13, '000 0.0'],
14651465
'scientific many decimal positions truncated' => ['000 0.000 00', 1e-7, '000 0.000 00'],
14661466
'scientific very many decimal positions truncated' => ['000 0.000 00', 1e-17, '000 0.000 00'],
1467+
[
1468+
'€ 1,111.12 ',
1469+
'1111.119',
1470+
'[$€-nl_NL]_(#,##0.00_);[$€-nl_NL] (#,##0.00)',
1471+
],
1472+
[
1473+
'€ (1,111.12)',
1474+
'-1111.119',
1475+
'[$€-nl_NL]_(#,##0.00_);[$€-nl_NL] (#,##0.00)',
1476+
],
1477+
[
1478+
'€ 1,111.12 ',
1479+
'1111.119',
1480+
'[$€-en_US]_(#,##0.00_);[$€-en_US] (#,##0.00)',
1481+
],
14671482
];

0 commit comments

Comments
 (0)