Skip to content

Commit

Permalink
Fix timestamp precision handling on PHP 7.3
Browse files Browse the repository at this point in the history
PHP 7 cannot handle timestame precision with more than six digits as
delivered by Fedora 6.5. This commit truncates the microsecond part
down this limit for `created` and `lastModified` repository properties.

Resolves https://git.slub-dresden.de/kitodo-publication/issues/-/issues/1910
  • Loading branch information
claussni committed Jun 14, 2024
1 parent fe6fb29 commit 6c7eec8
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Classes/Domain/Model/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use EWW\Dpf\Domain\Workflow\DocumentWorkflow;
use EWW\Dpf\Services\Api\InternalFormat;
use EWW\Dpf\Helper\DateTimePrecision;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

/**
Expand Down Expand Up @@ -768,7 +769,7 @@ public function getRemoteLastModDate()
*/
public function setRemoteLastModDate($remoteLastModDate)
{
$this->remoteLastModDate = $remoteLastModDate;
$this->remoteLastModDate = DateTimePrecision::reducePrecision($remoteLastModDate);
}

/**
Expand Down Expand Up @@ -1018,7 +1019,7 @@ public function getCreationDate(): string
*/
public function setCreationDate(string $creationDate): void
{
$this->creationDate = $creationDate;
$this->creationDate = DateTimePrecision::reducePrecision($creationDate);
}

/**
Expand Down
31 changes: 31 additions & 0 deletions Classes/Helper/DateTimePrecision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace EWW\Dpf\Helper;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

class DateTimePrecision
{

/**
* Reduce microsecond precision to value that can be handled by PHP 7.
*
* @param String dateTimeString Datetime string
* @return String Precision reduced datetime string
*/
public static function reducePrecision(String $dateTimeString)
{
return preg_replace('/(\.\d{6})\d+Z/', '$1Z', $dateTimeString);
}
}
46 changes: 46 additions & 0 deletions Tests/Unit/Helper/DateTimePrecisionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace EWW\Dpf\Tests\Unit\Helper;

/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/

use Nimut\TestingFramework\TestCase\UnitTestCase;
use EWW\Dpf\Helper\DateTimePrecision;

class DateTimePrecisionTest extends UnitTestCase {

/**
* @test
*/
public function Truncates_microseconds_precision_down_to_six_digits() {
$datetime = "2024-06-12T08:31:11.570355509Z";
$this->assertEquals("2024-06-12T08:31:11.570355Z", DateTimePrecision::reducePrecision($datetime));
}

/**
* @test
*/
public function No_truncation_on_six_digit_precision_() {
$datetime = "2024-06-12T08:31:11.570355Z";
$this->assertEquals("2024-06-12T08:31:11.570355Z", DateTimePrecision::reducePrecision($datetime));
}

/**
* @test
*/
public function No_truncation_on_less_than_six_digit_precision_() {
$datetime = "2024-06-12T08:31:11.570Z";
$this->assertEquals("2024-06-12T08:31:11.570Z", DateTimePrecision::reducePrecision($datetime));
}

}

0 comments on commit 6c7eec8

Please sign in to comment.