Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
JosueCA opened this issue Nov 8, 2024 · 0 comments
Open

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

JosueCA opened this issue Nov 8, 2024 · 0 comments

Comments

@JosueCA
Copy link

JosueCA commented Nov 8, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant