Fix time format for duration was incorrect

When using format `[h]:mm` it should convert to the "total hours:minutes"

Closes #666
Fixes #664
Fixes #446
Fixes #342
This commit is contained in:
GreatHumorist 2018-09-08 01:32:17 +08:00 committed by Adrien Crivelli
parent 39b573b29d
commit 699da09176
No known key found for this signature in database
GPG Key ID: B182FD79DC6DE92E
3 changed files with 117 additions and 85 deletions

View File

@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org).
- Improved performance when loading large spreadsheets - [#824](https://github.com/PHPOffice/PhpSpreadsheet/pull/824)
- Fix color from CSS when reading from HTML - [#831](https://github.com/PHPOffice/PhpSpreadsheet/pull/831)
- Fix infinite loop when reading invalid ODS files - [#832](https://github.com/PHPOffice/PhpSpreadsheet/pull/832)
- Fix time format for duration is incorrect - [#666](https://github.com/PHPOffice/PhpSpreadsheet/pull/666)
## [1.5.2] - 2018-11-25

View File

@ -467,6 +467,13 @@ class NumberFormat extends Supervisor
$block = strtr($block, self::$dateFormatReplacements);
if (!strpos($block, 'A')) {
// 24-hour time format
// when [h]:mm format, the [h] should replace to the hours of the value * 24
if (false !== strpos($block, '[h]')) {
$hours = (int) ($value * 24);
$block = str_replace('[h]', $hours, $block);
continue;
}
$block = strtr($block, self::$dateFormatReplacements24);
} else {
// 12-hour time format
@ -636,17 +643,17 @@ class NumberFormat extends Supervisor
// Save format with color information for later use below
$formatColor = $format;
// Strip color information
$color_regex = '/^\\[[a-zA-Z]+\\]/';
$format = preg_replace($color_regex, '', $format);
// Let's begin inspecting the format and converting the value to a formatted string
// Check for date/time characters (not inside quotes)
if (preg_match('/(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy](?=(?:[^"]|"[^"]*")*$)/miu', $format, $matches)) {
// datetime format
self::formatAsDate($value, $format);
} elseif (preg_match('/%$/', $format)) {
} else {
// Strip color information
$color_regex = '/^\\[[a-zA-Z]+\\]/';
$format = preg_replace($color_regex, '', $format);
if (preg_match('/%$/', $format)) {
// % number format
self::formatAsPercentage($value, $format);
} else {
@ -737,6 +744,7 @@ class NumberFormat extends Supervisor
}
}
}
}
// Additional formatting provided by callback function
if ($callBack !== null) {

View File

@ -72,4 +72,27 @@ return [
43332,
'[$-1010409]m/d/yyyy',
],
[
'27:15',
1.1354166666667,
'[h]:mm',
],
// Percentage
[
'12%',
0.12,
'0%',
],
// Simple color
[
'12345',
12345,
'[Green]General',
],
// Multiple colors
[
'12345',
12345,
'[Blue]0;[Red]0',
],
];