Update remaining references in the Financial Functions code to avoid calling deprecated methods (#1965)

* Update remaining references in the Financial Functions code to bypass deprecated date methods and use the new date function methods directly
This commit is contained in:
Mark Baker 2021-03-28 21:42:56 +02:00 committed by GitHub
parent e68978f1c7
commit b87f93f824
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 17 deletions

View File

@ -620,7 +620,7 @@ class Financial
if (($price <= 0) || ($redemption <= 0)) { if (($price <= 0) || ($redemption <= 0)) {
return Functions::NAN(); return Functions::NAN();
} }
$daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis); $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::funcYearFrac($settlement, $maturity, $basis);
if (!is_numeric($daysBetweenSettlementAndMaturity)) { if (!is_numeric($daysBetweenSettlementAndMaturity)) {
// return date error // return date error
return $daysBetweenSettlementAndMaturity; return $daysBetweenSettlementAndMaturity;
@ -810,7 +810,7 @@ class Financial
if (($investment <= 0) || ($redemption <= 0)) { if (($investment <= 0) || ($redemption <= 0)) {
return Functions::NAN(); return Functions::NAN();
} }
$daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis); $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::funcYearFrac($settlement, $maturity, $basis);
if (!is_numeric($daysBetweenSettlementAndMaturity)) { if (!is_numeric($daysBetweenSettlementAndMaturity)) {
// return date error // return date error
return $daysBetweenSettlementAndMaturity; return $daysBetweenSettlementAndMaturity;
@ -1451,7 +1451,7 @@ class Financial
if (($investment <= 0) || ($discount <= 0)) { if (($investment <= 0) || ($discount <= 0)) {
return Functions::NAN(); return Functions::NAN();
} }
$daysBetweenSettlementAndMaturity = DateTime::YEARFRAC($settlement, $maturity, $basis); $daysBetweenSettlementAndMaturity = DateTimeExcel\YearFrac::funcYearFrac($settlement, $maturity, $basis);
if (!is_numeric($daysBetweenSettlementAndMaturity)) { if (!is_numeric($daysBetweenSettlementAndMaturity)) {
// return date error // return date error
return $daysBetweenSettlementAndMaturity; return $daysBetweenSettlementAndMaturity;
@ -1639,9 +1639,10 @@ class Financial
$datesCount = count($dates); $datesCount = count($dates);
for ($i = 0; $i < $datesCount; ++$i) { for ($i = 0; $i < $datesCount; ++$i) {
$dates[$i] = DateTime::getDateValue($dates[$i]); try {
if (!is_numeric($dates[$i])) { $dates[$i] = DateTimeExcel\Helpers::getDateValue($dates[$i]);
return Functions::VALUE(); } catch (Exception $e) {
return $e->getMessage();
} }
} }
@ -1766,7 +1767,7 @@ class Financial
if ($valCount > 1 && ((min($values) > 0) || (max($values) < 0))) { if ($valCount > 1 && ((min($values) > 0) || (max($values) < 0))) {
return Functions::NAN(); return Functions::NAN();
} }
$date0 = DateTime::getDateValue($dates[0]); $date0 = DateTimeExcel\Helpers::getDateValue($dates[0]);
if (is_string($date0)) { if (is_string($date0)) {
return Functions::VALUE(); return Functions::VALUE();
} }
@ -1780,7 +1781,12 @@ class Financial
$values = Functions::flattenArray($values); $values = Functions::flattenArray($values);
$dates = Functions::flattenArray($dates); $dates = Functions::flattenArray($dates);
$valCount = count($values); $valCount = count($values);
$date0 = DateTime::getDateValue($dates[0]);
try {
$date0 = DateTimeExcel\Helpers::getDateValue($dates[0]);
} catch (Exception $e) {
return $e->getMessage();
}
$rslt = self::validateXnpv($rate, $values, $dates); $rslt = self::validateXnpv($rate, $values, $dates);
if ($rslt) { if ($rslt) {
return $rslt; return $rslt;
@ -1790,14 +1796,16 @@ class Financial
if (!is_numeric($values[$i])) { if (!is_numeric($values[$i])) {
return Functions::VALUE(); return Functions::VALUE();
} }
$datei = DateTime::getDateValue($dates[$i]);
if (is_string($datei)) { try {
return Functions::VALUE(); $datei = DateTimeExcel\Helpers::getDateValue($dates[$i]);
} catch (Exception $e) {
return $e->getMessage();
} }
if ($date0 > $datei) { if ($date0 > $datei) {
$dif = $ordered ? Functions::NAN() : -DateTime::DATEDIF($datei, $date0, 'd'); $dif = $ordered ? Functions::NAN() : -DateTimeExcel\DateDif::funcDateDif($datei, $date0, 'd');
} else { } else {
$dif = DateTime::DATEDIF($date0, $datei, 'd'); $dif = DateTimeExcel\DateDif::funcDateDif($date0, $datei, 'd');
} }
if (!is_numeric($dif)) { if (!is_numeric($dif)) {
return $dif; return $dif;

View File

@ -2,7 +2,7 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities; namespace PhpOffice\PhpSpreadsheet\Calculation\Financial\Securities;
use PhpOffice\PhpSpreadsheet\Calculation\DateTime; use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel\YearFrac;
use PhpOffice\PhpSpreadsheet\Calculation\Exception; use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions; use PhpOffice\PhpSpreadsheet\Calculation\Functions;
@ -76,12 +76,12 @@ class AccruedInterest
return $e->getMessage(); return $e->getMessage();
} }
$daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis); $daysBetweenIssueAndSettlement = YearFrac::funcYearFrac($issue, $settlement, $basis);
if (!is_numeric($daysBetweenIssueAndSettlement)) { if (!is_numeric($daysBetweenIssueAndSettlement)) {
// return date error // return date error
return $daysBetweenIssueAndSettlement; return $daysBetweenIssueAndSettlement;
} }
$daysBetweenFirstInterestAndSettlement = DateTime::YEARFRAC($firstinterest, $settlement, $basis); $daysBetweenFirstInterestAndSettlement = YearFrac::funcYearFrac($firstinterest, $settlement, $basis);
if (!is_numeric($daysBetweenFirstInterestAndSettlement)) { if (!is_numeric($daysBetweenFirstInterestAndSettlement)) {
// return date error // return date error
return $daysBetweenFirstInterestAndSettlement; return $daysBetweenFirstInterestAndSettlement;
@ -132,7 +132,7 @@ class AccruedInterest
return $e->getMessage(); return $e->getMessage();
} }
$daysBetweenIssueAndSettlement = DateTime::YEARFRAC($issue, $settlement, $basis); $daysBetweenIssueAndSettlement = YearFrac::funcYearFrac($issue, $settlement, $basis);
if (!is_numeric($daysBetweenIssueAndSettlement)) { if (!is_numeric($daysBetweenIssueAndSettlement)) {
// return date error // return date error
return $daysBetweenIssueAndSettlement; return $daysBetweenIssueAndSettlement;