Skip to content

Commit

Permalink
Fix returnUnformatted applying before date/time configuration values
Browse files Browse the repository at this point in the history
- Adjust logic to handle returnUnformatted differently for date/time values.
- Extend tests accordingly.
  • Loading branch information
adirfische committed Oct 26, 2021
1 parent f8a06e6 commit d49a04d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
11 changes: 9 additions & 2 deletions lib/NumberFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ public function formatValue($value, $xf_id)
}

// If formatting is not desired, return value as-is.
if ($this->configuration->getReturnUnformatted()
|| ($section->isPercentage() && $this->configuration->getReturnPercentageDecimal())) {
// Note: returnUnformatted for date/time values must be handled in applyDateTimeFormat(), due to additional constraints.
$return_unformatted = $this->configuration->getReturnUnformatted() && !$section->getDateTimeType();
$return_percentage_decimal = $section->isPercentage() && $this->configuration->getReturnPercentageDecimal();
if ($return_unformatted || $return_percentage_decimal) {
return $value;
}

Expand Down Expand Up @@ -904,6 +906,11 @@ private function applyDateTimeFormat($value, $section)
throw new RuntimeException('Specific datetime_type for format_index [' . $num_fmt_id . '] is unknown.');
}

// Check returnUnformatted HERE, so that returnDateTimeObjects and force...Format can take precedence.
if ($this->configuration->getReturnUnformatted()) {
return $value;
}

$output = '';
foreach ($section->getTokens() as $token) {
if ($token->isQuoted()) {
Expand Down
36 changes: 34 additions & 2 deletions tests/CellFormatConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public function dataProviderForFormatConfiguration()
)
),
'return datetime objects' => array(
(new ReaderConfiguration())->setReturnDateTimeObjects(true),
(new ReaderConfiguration())
->setReturnDateTimeObjects(true)
->setForceDateTimeFormat('Y-m-d His'), // Should have no effect, overruled by returnDateTimeObjects.
array_replace(
$return_base,
array(
1 => new DateTime('2017-08-20', new DateTimeZone('UTC')),
2 => new DateTime('1899-12-31 22:30:00', new DateTimeZone('UTC')), // From base date: 1900-01-00
2 => new DateTime('1899-12-31 22:30:00', new DateTimeZone('UTC')), // From base date: 1900-01-00 <- note the 0 day
3 => new DateTime('2017-08-20 22:30:00', new DateTimeZone('UTC'))
)
)
Expand Down Expand Up @@ -88,6 +90,36 @@ public function dataProviderForFormatConfiguration()
5 => '5.0000000000000001E-3' // Common Excel problem. Percentage values are divided by 100, which can cause floating point issues.
)
)
),
'return unformatted is overruled by datetime force format' => array(
(new ReaderConfiguration())
->setReturnUnformatted(true)
->setForceDateTimeFormat('Y-m-d His'),
array_replace(
$return_base,
array(
1 => '42967', // date value, not datetime value. Therefore, returnUnformatted applies here.
2 => '0.9375', // Same situation for time values.
3 => '2017-08-20 223000', // datetime value, forceDateTimeFormat applies here.
4 => '25.5',
5 => '0.5'
)
)
),
'return unformatted is overruled by return datetime objects' => array(
(new ReaderConfiguration())
->setReturnUnformatted(true)
->setReturnDateTimeObjects(true),
array_replace(
$return_base,
array(
1 => new DateTime('2017-08-20', new DateTimeZone('UTC')),
2 => new DateTime('1899-12-31 22:30:00', new DateTimeZone('UTC')), // From base date: 1900-01-00 <- note the 0 day
3 => new DateTime('2017-08-20 22:30:00', new DateTimeZone('UTC')),
4 => '25.5',
5 => '0.5'
)
)
)
);
}
Expand Down

0 comments on commit d49a04d

Please sign in to comment.