Scrutinizer's Turn

3 minor errors that hadn't blocked the request.
This commit is contained in:
Owen Leibman 2021-05-23 17:24:26 -07:00
parent 98e30e3bf1
commit 1bce0e193d
5 changed files with 19 additions and 14 deletions

View File

@ -1,6 +1,7 @@
<?php
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Shared\Date;
require __DIR__ . '/../Header.php';
@ -18,9 +19,8 @@ $helper->log('<b>Document Creator: </b>' . $creator);
// Read the Date when the workbook was created (as a PHP timestamp value)
$creationDatestamp = $spreadsheet->getProperties()->getCreated();
// Format the date and time using the standard PHP date() function
$creationDate = date('l, d<\s\up>S</\s\up> F Y', $creationDatestamp);
$creationTime = date('g:i A', $creationDatestamp);
$creationDate = Date::formattedDateTimeFromTimestamp("$creationDatestamp", 'l, d<\s\up>S</\s\up> F Y');
$creationTime = Date::formattedDateTimeFromTimestamp("$creationDatestamp", 'g:i A');
$helper->log('<b>Created On: </b>' . $creationDate . ' at ' . $creationTime);
// Read the name of the last person to modify this workbook
@ -30,8 +30,8 @@ $helper->log('<b>Last Modified By: </b>' . $modifiedBy);
// Read the Date when the workbook was last modified (as a PHP timestamp value)
$modifiedDatestamp = $spreadsheet->getProperties()->getModified();
// Format the date and time using the standard PHP date() function
$modifiedDate = date('l, d<\s\up>S</\s\up> F Y', $modifiedDatestamp);
$modifiedTime = date('g:i A', $modifiedDatestamp);
$modifiedDate = Date::formattedDateTimeFromTimestamp("$modifiedDatestamp", 'l, d<\s\up>S</\s\up> F Y');
$modifiedTime = Date::formattedDateTimeFromTimestamp("$modifiedDatestamp", 'g:i A');
$helper->log('<b>Last Modified On: </b>' . $modifiedDate . ' at ' . $modifiedTime);
// Read the workbook title property

View File

@ -501,14 +501,17 @@ class Date
return $day;
}
public static function dateTimeFromTimestamp(string $date): DateTime
public static function dateTimeFromTimestamp(string $date, ?DateTimeZone $timeZone = null): DateTime
{
return DateTime::createFromFormat('U', $date) ?: new DateTime();
$dtobj = DateTime::createFromFormat('U', $date) ?: new DateTime();
$dtobj->setTimeZone($timeZone ?? self::getDefaultOrLocalTimezone());
return $dtobj;
}
public static function formattedDateTimeFromTimestamp(string $date, string $format): string
public static function formattedDateTimeFromTimestamp(string $date, string $format, ?DateTimeZone $timeZone = null): string
{
$dtobj = self::dateTimeFromTimestamp($date);
$dtobj = self::dateTimeFromTimestamp($date, $timeZone);
return $dtobj->format($format);
}

View File

@ -82,7 +82,7 @@ class Meta extends WriterPart
return $objWriter->getData();
}
private function writeDocPropsCustom(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
private static function writeDocPropsCustom(XMLWriter $objWriter, Spreadsheet $spreadsheet): void
{
$customPropertyList = $spreadsheet->getProperties()->getCustomProperties();
foreach ($customPropertyList as $key => $customProperty) {
@ -106,7 +106,7 @@ class Meta extends WriterPart
break;
case Properties::PROPERTY_TYPE_DATE:
$objWriter->writeAttribute('meta:value-type', 'date');
$dtobj = Date::dateTimeFromTimestamp($propertyValue);
$dtobj = Date::dateTimeFromTimestamp($propertyValue ?? 0);
$objWriter->writeRawData($dtobj->format(DATE_W3C));
break;

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Document;
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PHPUnit\Framework\TestCase;
@ -163,7 +164,7 @@ class PropertiesTest extends TestCase
self::assertSame($expectedType, $this->properties->getCustomPropertyType($propertyName));
$result = $this->properties->getCustomPropertyValue($propertyName);
if ($expectedType === Properties::PROPERTY_TYPE_DATE) {
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d');
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC'));
}
self::assertSame($expectedValue, $result);
}

View File

@ -2,6 +2,7 @@
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
use DateTimeZone;
use PhpOffice\PhpSpreadsheet\Document\Properties;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Shared\Date;
@ -44,7 +45,7 @@ class PropertiesTest extends AbstractFunctional
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
$result = $properties->getCustomPropertyValue($propertyName);
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d');
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC'));
}
self::assertSame($testData['value'], $result);
}
@ -86,7 +87,7 @@ class PropertiesTest extends AbstractFunctional
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
$result = $properties->getCustomPropertyValue($propertyName);
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d');
$result = Date::formattedDateTimeFromTimestamp("$result", 'Y-m-d', new DateTimeZone('UTC'));
}
self::assertSame($testData['value'], $result);
}