Skip to content

Commit 241e82b

Browse files
authored
Unexpected Format in Timestamp (#2332)
See issue #2331. Timestamp is expected in format yyyy-mm-dd (plus other information), with the expectation that month and day are 2 digits zero-filled on the left if needed. The user's file instead used a space rather than zero as filler. Although I don't know how the unexpected timestamp was created, it was easy enough to alter the timestamp in an otherwise normal spreadsheet, and use that file as a test case.
1 parent 3db0b2a commit 241e82b

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

src/PhpSpreadsheet/Document/Properties.php

+2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ private static function intOrFloatTimestamp($timestamp)
172172
$timestamp = (float) $timestamp;
173173
} else {
174174
$timestamp = preg_replace('/[.][0-9]*$/', '', $timestamp) ?? '';
175+
$timestamp = preg_replace('/^(\\d{4})- (\\d)/', '$1-0$2', $timestamp) ?? '';
176+
$timestamp = preg_replace('/^(\\d{4}-\\d{2})- (\\d)/', '$1-0$2', $timestamp) ?? '';
175177
$timestamp = (float) (new DateTime($timestamp))->format('U');
176178
}
177179
}

src/PhpSpreadsheet/Reader/Xlsx/Properties.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public function readCoreProperties(string $propertyData): void
5252

5353
$this->docProps->setCreator((string) self::getArrayItem($xmlCore->xpath('dc:creator')));
5454
$this->docProps->setLastModifiedBy((string) self::getArrayItem($xmlCore->xpath('cp:lastModifiedBy')));
55-
$this->docProps->setCreated((int) strtotime((string) self::getArrayItem($xmlCore->xpath('dcterms:created')))); //! respect xsi:type
56-
$this->docProps->setModified((int) strtotime((string) self::getArrayItem($xmlCore->xpath('dcterms:modified')))); //! respect xsi:type
55+
$this->docProps->setCreated((string) self::getArrayItem($xmlCore->xpath('dcterms:created'))); //! respect xsi:type
56+
$this->docProps->setModified((string) self::getArrayItem($xmlCore->xpath('dcterms:modified'))); //! respect xsi:type
5757
$this->docProps->setTitle((string) self::getArrayItem($xmlCore->xpath('dc:title')));
5858
$this->docProps->setDescription((string) self::getArrayItem($xmlCore->xpath('dc:description')));
5959
$this->docProps->setSubject((string) self::getArrayItem($xmlCore->xpath('dc:subject')));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PhpOffice\PhpSpreadsheetTests\Reader\Xlsx;
4+
5+
use DateTimeZone;
6+
use PhpOffice\PhpSpreadsheet\IOFactory;
7+
use PhpOffice\PhpSpreadsheet\Shared\Date;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class Issue2331Test extends TestCase
11+
{
12+
public function testIssue2331(): void
13+
{
14+
// Leading space instead of 0 in month and/or day of timestamp.
15+
$filename = 'tests/data/Reader/XLSX/issue.2331c.xlsx';
16+
$reader = IOFactory::createReader('Xlsx');
17+
$spreadsheet = $reader->load($filename);
18+
$properties = $spreadsheet->getProperties();
19+
$created = (string) $properties->getCreated();
20+
$modified = (string) $properties->getModified();
21+
22+
self::assertEquals('2021-08-02', Date::formattedDateTimeFromTimestamp($created, 'Y-m-d', new DateTimeZone('UTC')));
23+
self::assertEquals('2021-09-03', Date::formattedDateTimeFromTimestamp($modified, 'Y-m-d', new DateTimeZone('UTC')));
24+
$spreadsheet->disconnectWorksheets();
25+
}
26+
}
8.22 KB
Binary file not shown.

0 commit comments

Comments
 (0)