Skip to content

Error "Cannot create date/time object for timestamp xxxxxxxxxx" #98

@JosueCA

Description

@JosueCA
Q A
Library version(s) affected: 3.3.3
PHP version(s): 7.4.33
OS (with bit depth): Linux Mint 21.2 x86_64

Description
When i try to open any zip file this error is produced in my application:
"Cannot create date/time object for timestamp 1730994211"

I found the error is produced in the function 'ntfsTimeToDateTime' of the class 'PhpZip\Model\Extra\Fields\NtfsExtraField' because of the Locale used by my application

How to reproduce

Simplified code example

composer.json

{
	"require" : {
		"nelexa/zip" : "3.3.3"
	}
}

test.php

<?php

require_once(__DIR__ . '/vendor/autoload.php');

// THIS LOCALE PRODUCES THE ERROR
setlocale(LC_ALL, "es_ES.utf-8");

$zipInput = "test.zip";
$outDirectory = __DIR__ . DIRECTORY_SEPARATOR;

$zipFile = new \PhpZip\ZipFile();
try{
    $zipFile->openFile($zipInput);
    $zipFile->extractTo($outDirectory);
}
catch(\PhpZip\Exception\ZipException $e){
    throw new \Exception("ZipException error: " . $e->getMessage());
}
finally{
    $zipFile->close();
}

echo "DONE.";

Install dependency with composer
Create a zip file called 'test.zip' in the same directory as the 'test.php' script
Execute

php test.php

Possible Solution
When using sprintf('%.6f', $timestamp), use the 'locale independent' for float conversions -> sprintf('%.6F', $timestamp)

public static function ntfsTimeToDateTime($ntfsTime)
    {
        $timestamp = self::ntfsTimeToTimestamp($ntfsTime);
        $dateTime = \DateTimeImmutable::createFromFormat('U.u', sprintf('%.6F', $timestamp));

        if ($dateTime === false) {
            throw new InvalidArgumentException('Cannot create date/time object for timestamp ' . $timestamp);
        }

        return $dateTime;
    }

Additional context
https://php.watch/versions/8.0/float-to-string-locale-independent

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions