mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-13 03:26:26 +00:00
More Data in Parameters, Less in Static Variables
This commit is contained in:
@@ -46,13 +46,22 @@ class DateValue
|
||||
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $dateValue);
|
||||
}
|
||||
|
||||
return self::fromString2($dateValue, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<mixed>|DateTime|float|int|string Excel date/time serial value, PHP date/time serial value or PHP date/time object,
|
||||
* depending on the value of the ReturnDateType flag
|
||||
*/
|
||||
public static function fromString2(null|string|int|bool|float $dateValue, ?int $calendar = null): array|string|float|int|DateTime
|
||||
{
|
||||
// try to parse as date iff there is at least one digit
|
||||
if (is_string($dateValue) && preg_match('/\d/', $dateValue) !== 1) {
|
||||
return ExcelError::VALUE();
|
||||
}
|
||||
|
||||
$dti = new DateTimeImmutable();
|
||||
$baseYear = SharedDateHelper::getExcelCalendar();
|
||||
$baseYear = $calendar ?? SharedDateHelper::getExcelCalendar();
|
||||
$dateValue = trim((string) $dateValue, '"');
|
||||
// Strip any ordinals because they're allowed in Excel (English only)
|
||||
$dateValue = (string) preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue);
|
||||
@@ -160,9 +169,9 @@ class DateValue
|
||||
$day = self::getInt($PHPDateArray, 'day');
|
||||
$year = self::getInt($PHPDateArray, 'year');
|
||||
if (!checkdate($month, $day, $year)) {
|
||||
return ($year === 1900 && $month === 2 && $day === 29) ? Helpers::returnIn3FormatsFloat(60.0) : ExcelError::VALUE();
|
||||
return ($year === 1900 && $month === 2 && $day === 29) ? Helpers::returnIn3FormatsFloat(60.0, calendar: $baseYear) : ExcelError::VALUE();
|
||||
}
|
||||
$retValue = Helpers::returnIn3FormatsArray($PHPDateArray, true);
|
||||
$retValue = Helpers::returnIn3FormatsArray($PHPDateArray, true, calendar: $baseYear);
|
||||
}
|
||||
|
||||
return $retValue;
|
||||
|
||||
@@ -30,10 +30,10 @@ class Helpers
|
||||
*
|
||||
* @return float Excel date/time serial value
|
||||
*/
|
||||
public static function getDateValue(mixed $dateValue, bool $allowBool = true): float
|
||||
public static function getDateValue(mixed $dateValue, bool $allowBool = true, ?int $calendar = null): float
|
||||
{
|
||||
if (is_object($dateValue)) {
|
||||
$retval = SharedDateHelper::PHPToExcel($dateValue);
|
||||
$retval = SharedDateHelper::PHPToExcel($dateValue, calendar: $calendar);
|
||||
if (is_bool($retval)) {
|
||||
throw new Exception(ExcelError::VALUE());
|
||||
}
|
||||
@@ -41,7 +41,7 @@ class Helpers
|
||||
return $retval;
|
||||
}
|
||||
|
||||
self::nullFalseTrueToNumber($dateValue, $allowBool);
|
||||
self::nullFalseTrueToNumber($dateValue, $allowBool, $calendar);
|
||||
if (!is_numeric($dateValue)) {
|
||||
$saveReturnDateType = Functions::getReturnDateType();
|
||||
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
|
||||
@@ -58,7 +58,7 @@ class Helpers
|
||||
}
|
||||
|
||||
try {
|
||||
SharedDateHelper::excelToDateTimeObject((float) $dateValue);
|
||||
SharedDateHelper::excelToDateTimeObject((float) $dateValue, calendar: $calendar);
|
||||
} catch (Throwable) {
|
||||
throw new Exception(ExcelError::NAN());
|
||||
}
|
||||
@@ -87,10 +87,10 @@ class Helpers
|
||||
*
|
||||
* @param float|int $dateValue date to be adjusted
|
||||
*/
|
||||
public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0): DateTime
|
||||
public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0, ?int $calendar = null): DateTime
|
||||
{
|
||||
// Execute function
|
||||
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
|
||||
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue, calendar: $calendar);
|
||||
$oMonth = (int) $PHPDateObject->format('m');
|
||||
$oYear = (int) $PHPDateObject->format('Y');
|
||||
|
||||
@@ -141,7 +141,7 @@ class Helpers
|
||||
*
|
||||
* @param array{year: int, month: int, day: int, hour: int, minute: int, second: int} $dateArray
|
||||
*/
|
||||
public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = false): DateTime|float|int
|
||||
public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = false, ?int $calendar = null): DateTime|float|int
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
|
||||
@@ -161,58 +161,60 @@ class Helpers
|
||||
$dateArray['day'],
|
||||
$dateArray['hour'],
|
||||
$dateArray['minute'],
|
||||
$dateArray['second']
|
||||
$dateArray['second'],
|
||||
calendar: $calendar
|
||||
);
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return $noFrac ? floor($excelDateValue) : $excelDateValue;
|
||||
}
|
||||
// RETURNDATE_UNIX_TIMESTAMP)
|
||||
|
||||
return SharedDateHelper::excelToTimestamp($excelDateValue);
|
||||
return SharedDateHelper::excelToTimestamp($excelDateValue, calendar: $calendar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result in one of three formats.
|
||||
*/
|
||||
public static function returnIn3FormatsFloat(float $excelDateValue): float|int|DateTime
|
||||
public static function returnIn3FormatsFloat(float $excelDateValue, ?int $calendar = null): float|int|DateTime
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return $excelDateValue;
|
||||
}
|
||||
if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
|
||||
return SharedDateHelper::excelToTimestamp($excelDateValue);
|
||||
return SharedDateHelper::excelToTimestamp($excelDateValue, calendar: $calendar);
|
||||
}
|
||||
// RETURNDATE_PHP_DATETIME_OBJECT
|
||||
|
||||
return SharedDateHelper::excelToDateTimeObject($excelDateValue);
|
||||
return SharedDateHelper::excelToDateTimeObject($excelDateValue, calendar: $calendar);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return result in one of three formats.
|
||||
*/
|
||||
public static function returnIn3FormatsObject(DateTime $PHPDateObject): DateTime|float|int
|
||||
public static function returnIn3FormatsObject(DateTime $PHPDateObject, ?int $calendar = null): DateTime|float|int
|
||||
{
|
||||
$retType = Functions::getReturnDateType();
|
||||
if ($retType === Functions::RETURNDATE_PHP_DATETIME_OBJECT) {
|
||||
return $PHPDateObject;
|
||||
}
|
||||
if ($retType === Functions::RETURNDATE_EXCEL) {
|
||||
return (float) SharedDateHelper::PHPToExcel($PHPDateObject);
|
||||
return (float) SharedDateHelper::PHPToExcel($PHPDateObject, calendar: $calendar);
|
||||
}
|
||||
// RETURNDATE_UNIX_TIMESTAMP
|
||||
$stamp = SharedDateHelper::PHPToExcel($PHPDateObject);
|
||||
$stamp = SharedDateHelper::PHPToExcel($PHPDateObject, calendar: $calendar);
|
||||
$stamp = is_bool($stamp) ? ((int) $stamp) : $stamp;
|
||||
|
||||
return SharedDateHelper::excelToTimestamp($stamp);
|
||||
return SharedDateHelper::excelToTimestamp($stamp, calendar: $calendar);
|
||||
}
|
||||
|
||||
private static function baseDate(): int
|
||||
private static function baseDate(?int $calendar): int
|
||||
{
|
||||
if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) {
|
||||
return 0;
|
||||
}
|
||||
if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904) {
|
||||
$calendar ??= SharedDateHelper::getExcelCalendar();
|
||||
if ($calendar === SharedDateHelper::CALENDAR_MAC_1904) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -222,10 +224,10 @@ class Helpers
|
||||
/**
|
||||
* Many functions accept null/false/true argument treated as 0/0/1.
|
||||
*/
|
||||
public static function nullFalseTrueToNumber(mixed &$number, bool $allowBool = true): void
|
||||
public static function nullFalseTrueToNumber(mixed &$number, bool $allowBool = true, ?int $calendar = null): void
|
||||
{
|
||||
$number = Functions::flattenSingleValue($number);
|
||||
$nullVal = self::baseDate();
|
||||
$nullVal = self::baseDate($calendar);
|
||||
if ($number === null) {
|
||||
$number = $nullVal;
|
||||
} elseif ($allowBool && is_bool($number)) {
|
||||
|
||||
@@ -488,16 +488,7 @@ class Date
|
||||
return false;
|
||||
}
|
||||
|
||||
$hold = self::$excelCalendar;
|
||||
|
||||
try {
|
||||
if ($calendar !== null) {
|
||||
self::$excelCalendar = $calendar;
|
||||
}
|
||||
$dateValueNew = DateTimeExcel\DateValue::fromString($dateValue);
|
||||
} finally {
|
||||
self::$excelCalendar = $hold;
|
||||
}
|
||||
$dateValueNew = DateTimeExcel\DateValue::fromString2($dateValue, $calendar);
|
||||
|
||||
if (!is_float($dateValueNew)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user