Skip to content

Commit

Permalink
Use correct Exception, ensure that Shared\Date is referenced by a syn…
Browse files Browse the repository at this point in the history
…onym to ensure clarity, and ensure case-sensitivity for class references
  • Loading branch information
MarkBaker authored and MarkBaker committed May 12, 2021
1 parent 765d458 commit 9b34f87
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Constants.php
Expand Up @@ -22,6 +22,7 @@ class Constants
const DOW_FRIDAY = 6;
const DOW_SATURDAY = 7;
const STARTWEEK_MONDAY_ISO = 21;

const METHODARR = [
self::STARTWEEK_SUNDAY => self::DOW_SUNDAY,
self::DOW_MONDAY,
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Date.php
Expand Up @@ -2,7 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
Expand Down Expand Up @@ -102,7 +102,7 @@ private static function getYear($year, int $baseYear): int
$year += 1900;
}

return $year;
return (int) $year;
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/DateParts.php
Expand Up @@ -4,7 +4,7 @@

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class DateParts
{
Expand Down Expand Up @@ -36,7 +36,7 @@ public static function day($dateValue)
}

// Execute function
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);

return (int) $PHPDateObject->format('j');
}
Expand All @@ -62,12 +62,12 @@ public static function month($dateValue)
} catch (Exception $e) {
return $e->getMessage();
}
if ($dateValue < 1 && Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900) {
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
return 1;
}

// Execute function
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);

return (int) $PHPDateObject->format('n');
}
Expand All @@ -94,11 +94,11 @@ public static function year($dateValue)
return $e->getMessage();
}

if ($dateValue < 1 && Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900) {
if ($dateValue < 1 && SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900) {
return 1900;
}
// Execute function
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);

return (int) $PHPDateObject->format('Y');
}
Expand All @@ -110,7 +110,7 @@ public static function year($dateValue)
private static function weirdCondition($dateValue): int
{
// Excel does not treat 0 consistently for DAY vs. (MONTH or YEAR)
if (Date::getExcelCalendar() === DATE::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_WINDOWS_1900 && Functions::getCompatibilityMode() == Functions::COMPATIBILITY_EXCEL) {
if (is_bool($dateValue)) {
return (int) $dateValue;
}
Expand Down
4 changes: 2 additions & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/DateValue.php
Expand Up @@ -4,7 +4,7 @@

use DateTimeImmutable;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class DateValue
{
Expand Down Expand Up @@ -36,7 +36,7 @@ class DateValue
public static function fromString($dateValue)
{
$dti = new DateTimeImmutable();
$baseYear = Date::getExcelCalendar();
$baseYear = SharedDateHelper::getExcelCalendar();
$dateValue = trim(Functions::flattenSingleValue($dateValue), '"');
// Strip any ordinals because they're allowed in Excel (English only)
$dateValue = preg_replace('/(\d)(st|nd|rd|th)([ -\/])/Ui', '$1$3', $dateValue) ?? '';
Expand Down
8 changes: 4 additions & 4 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Days.php
Expand Up @@ -3,9 +3,9 @@
namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use DateTimeInterface;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class Days
{
Expand Down Expand Up @@ -34,8 +34,8 @@ public static function between($endDate, $startDate)
}

// Execute function
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);

$days = Functions::VALUE();
$diff = $PHPStartDateObject->diff($PHPEndDateObject);
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Days360.php
Expand Up @@ -4,7 +4,7 @@

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class Days360
{
Expand Down Expand Up @@ -50,12 +50,12 @@ public static function between($startDate = 0, $endDate = 0, $method = false)
}

// Execute function
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
$startDay = $PHPStartDateObject->format('j');
$startMonth = $PHPStartDateObject->format('n');
$startYear = $PHPStartDateObject->format('Y');

$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
$endDay = $PHPEndDateObject->format('j');
$endMonth = $PHPEndDateObject->format('n');
$endYear = $PHPEndDateObject->format('Y');
Expand Down
8 changes: 4 additions & 4 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Difference.php
Expand Up @@ -4,9 +4,9 @@

use DateInterval;
use DateTime;
use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class Difference
{
Expand All @@ -33,12 +33,12 @@ public static function interval($startDate, $endDate, $unit = 'D')
}

// Execute function
$PHPStartDateObject = Date::excelToDateTimeObject($startDate);
$PHPStartDateObject = SharedDateHelper::excelToDateTimeObject($startDate);
$startDays = (int) $PHPStartDateObject->format('j');
$startMonths = (int) $PHPStartDateObject->format('n');
$startYears = (int) $PHPStartDateObject->format('Y');

$PHPEndDateObject = Date::excelToDateTimeObject($endDate);
$PHPEndDateObject = SharedDateHelper::excelToDateTimeObject($endDate);
$endDays = (int) $PHPEndDateObject->format('j');
$endMonths = (int) $PHPEndDateObject->format('n');
$endYears = (int) $PHPEndDateObject->format('Y');
Expand Down
22 changes: 11 additions & 11 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Helpers.php
Expand Up @@ -5,7 +5,7 @@
use DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class Helpers
{
Expand All @@ -31,7 +31,7 @@ public static function isLeapYear($year): bool
public static function getDateValue($dateValue, bool $allowBool = true): float
{
if (is_object($dateValue)) {
$retval = Date::PHPToExcel($dateValue);
$retval = SharedDateHelper::PHPToExcel($dateValue);
if (is_bool($retval)) {
throw new Exception(Functions::VALUE());
}
Expand Down Expand Up @@ -81,7 +81,7 @@ public static function getTimeValue($timeValue)
public static function adjustDateByMonths($dateValue = 0, float $adjustmentMonths = 0): DateTime
{
// Execute function
$PHPDateObject = Date::excelToDateTimeObject($dateValue);
$PHPDateObject = SharedDateHelper::excelToDateTimeObject($dateValue);
$oMonth = (int) $PHPDateObject->format('m');
$oYear = (int) $PHPDateObject->format('Y');

Expand Down Expand Up @@ -149,7 +149,7 @@ public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = fa
);
}
$excelDateValue =
Date::formattedPHPToExcel(
SharedDateHelper::formattedPHPToExcel(
$dateArray['year'],
$dateArray['month'],
$dateArray['day'],
Expand All @@ -162,7 +162,7 @@ public static function returnIn3FormatsArray(array $dateArray, bool $noFrac = fa
}
// RETURNDATE_UNIX_TIMESTAMP)

return (int) Date::excelToTimestamp($excelDateValue);
return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
}

/**
Expand All @@ -177,11 +177,11 @@ public static function returnIn3FormatsFloat(float $excelDateValue)
return $excelDateValue;
}
if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
return (int) Date::excelToTimestamp($excelDateValue);
return (int) SharedDateHelper::excelToTimestamp($excelDateValue);
}
// RETURNDATE_PHP_DATETIME_OBJECT

return Date::excelToDateTimeObject($excelDateValue);
return SharedDateHelper::excelToDateTimeObject($excelDateValue);
}

/**
Expand All @@ -196,21 +196,21 @@ public static function returnIn3FormatsObject(DateTime $PHPDateObject)
return $PHPDateObject;
}
if ($retType === Functions::RETURNDATE_EXCEL) {
return (float) Date::PHPToExcel($PHPDateObject);
return (float) SharedDateHelper::PHPToExcel($PHPDateObject);
}
// RETURNDATE_UNIX_TIMESTAMP
$stamp = Date::PHPToExcel($PHPDateObject);
$stamp = SharedDateHelper::PHPToExcel($PHPDateObject);
$stamp = is_bool($stamp) ? ((int) $stamp) : $stamp;

return (int) Date::excelToTimestamp($stamp);
return (int) SharedDateHelper::excelToTimestamp($stamp);
}

private static function baseDate(): int
{
if (Functions::getCompatibilityMode() === Functions::COMPATIBILITY_OPENOFFICE) {
return 0;
}
if (Date::getExcelCalendar() === Date::CALENDAR_MAC_1904) {
if (SharedDateHelper::getExcelCalendar() === SharedDateHelper::CALENDAR_MAC_1904) {
return 0;
}

Expand Down
3 changes: 1 addition & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Month.php
Expand Up @@ -2,8 +2,7 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use Exception;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;

class Month
{
Expand Down
3 changes: 1 addition & 2 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/NetworkDays.php
Expand Up @@ -2,9 +2,8 @@

namespace PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;

use Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;

class NetworkDays
{
Expand Down
10 changes: 5 additions & 5 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/Time.php
Expand Up @@ -5,7 +5,7 @@
use DateTime;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class Time
{
Expand Down Expand Up @@ -57,13 +57,13 @@ public static function fromHMS($hour, $minute, $second)
// Execute function
$retType = Functions::getReturnDateType();
if ($retType === Functions::RETURNDATE_EXCEL) {
$calendar = Date::getExcelCalendar();
$date = (int) ($calendar !== Date::CALENDAR_WINDOWS_1900);
$calendar = SharedDateHelper::getExcelCalendar();
$date = (int) ($calendar !== SharedDateHelper::CALENDAR_WINDOWS_1900);

return (float) Date::formattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
return (float) SharedDateHelper::formattedPHPToExcel($calendar, 1, $date, $hour, $minute, $second);
}
if ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
return (int) Date::excelToTimestamp(Date::formattedPHPToExcel(1970, 1, 1, $hour, $minute, $second)); // -2147468400; // -2147472000 + 3600
return (int) SharedDateHelper::excelToTimestamp(SharedDateHelper::formattedPHPToExcel(1970, 1, 1, $hour, $minute, $second)); // -2147468400; // -2147472000 + 3600
}
// RETURNDATE_PHP_DATETIME_OBJECT
// Hour has already been normalized (0-23) above
Expand Down
8 changes: 4 additions & 4 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeParts.php
Expand Up @@ -4,7 +4,7 @@

use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class TimeParts
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public static function hour($timeValue)

// Execute function
$timeValue = fmod($timeValue, 1);
$timeValue = Date::excelToDateTimeObject($timeValue);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);

return (int) $timeValue->format('H');
}
Expand Down Expand Up @@ -71,7 +71,7 @@ public static function minute($timeValue)

// Execute function
$timeValue = fmod($timeValue, 1);
$timeValue = Date::excelToDateTimeObject($timeValue);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);

return (int) $timeValue->format('i');
}
Expand Down Expand Up @@ -105,7 +105,7 @@ public static function second($timeValue)

// Execute function
$timeValue = fmod($timeValue, 1);
$timeValue = Date::excelToDateTimeObject($timeValue);
$timeValue = SharedDateHelper::excelToDateTimeObject($timeValue);

return (int) $timeValue->format('s');
}
Expand Down
6 changes: 3 additions & 3 deletions src/PhpSpreadsheet/Calculation/DateTimeExcel/TimeValue.php
Expand Up @@ -4,7 +4,7 @@

use Datetime;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Shared\Date as SharedDateHelper;

class TimeValue
{
Expand Down Expand Up @@ -44,13 +44,13 @@ public static function fromString($timeValue)
$retValue = Functions::VALUE();
if (($PHPDateArray !== false) && ($PHPDateArray['error_count'] == 0)) {
// OpenOffice-specific code removed - it works just like Excel
$excelDateValue = Date::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1;
$excelDateValue = SharedDateHelper::formattedPHPToExcel(1900, 1, 1, $PHPDateArray['hour'], $PHPDateArray['minute'], $PHPDateArray['second']) - 1;

$retType = Functions::getReturnDateType();
if ($retType === Functions::RETURNDATE_EXCEL) {
$retValue = (float) $excelDateValue;
} elseif ($retType === Functions::RETURNDATE_UNIX_TIMESTAMP) {
$retValue = (int) $phpDateValue = Date::excelToTimestamp($excelDateValue + 25569) - 3600;
$retValue = (int) $phpDateValue = SharedDateHelper::excelToTimestamp($excelDateValue + 25569) - 3600;
} else {
$retValue = new DateTime('1900-01-01 ' . $PHPDateArray['hour'] . ':' . $PHPDateArray['minute'] . ':' . $PHPDateArray['second']);
}
Expand Down

0 comments on commit 9b34f87

Please sign in to comment.