[php-src] PR #12413: Adds DateTime[Immutable]::createFromTimestamp

From: Date: Wed, 11 Oct 2023 09:43:53 +0000
Subject: [php-src] PR #12413: Adds DateTime[Immutable]::createFromTimestamp
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-23859@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/12413 Author: marc-mabe This creates the following functions: ```php DateTime::createFromTimestamp(int|float $timestamp): DateTime|false DateTimeImmutable::createFromTimestamp(int|float $timestamp): DateTimeImmutable|false date_create_from_timestamp(int|float $timestamp): DateTime|false date_create_immutable_from_timestamp(int|float $timestamp): DateTimeImmutable|false ``` The reason for these functions is the very common case of creating a DateTime[Immutable] object from a unix timestamp. This currently only works by: * special timestamp constructor format @<timestamp> * needs to be known by studying https://www.php.net/manual/en/datetime.formats.php * requires stringifying the timestamp * requires handling edge cases like avoiding scientific notation of floating points * createFromFormat with U / U.u * same as @<timstamp> but adds additional differences on integer vs. float * (new DateTime(...))->setTimestamp() * needs instance beforehand which does a current time lookup * setTimestamp supports integers only (This probably should allow floats as well in a separate PR) The new functions avoid the above issues, are self explained and are much faster for this common case as well. The functions can only return false in case of special floating point numbers or not within range of zend_long. Bench (doesn't even handle scientific notation of floating points): ```php <?php $calls = 10000; $time = time(); $microtime = microtime(true); $utc = new DateTimeZone('+00:00'); function p(float $start, float $end, int $calls) { printf( "\t%ss for %s calls (%Fns per call)\n", ($end - $start) / 1000000000, $calls, ($end - $start) / $calls, ); } foreach ([$time, $microtime] as $t) { echo '$t = ' . var_export($t, true) . "\n"; echo 'new DateTime("@" . $t, $utc): ' . "\n"; $start = hrtime(true); for ($i = 0; $i < $calls; $i += 1) { $tmp = new DateTime("@" . $t, $utc); } $end = hrtime(true); p($start, $end, $calls); //var_dump($tmp); if (is_float($t)) { echo 'DateTime::createFromFormat("U.u", $t, $utc): ' . "\n"; $start = hrtime(true); for ($i = 0; $i < $calls; $i += 1) { $tmp = DateTime::createFromFormat("U.u", $t, $utc); } $end = hrtime(true); p($start, $end, $calls); } else { echo 'DateTime::createFromFormat("U", $t, $utc): ' . "\n"; $start = hrtime(true); for ($i = 0; $i < $calls; $i += 1) { $tmp = DateTime::createFromFormat("U", $t, $utc); } $end = hrtime(true); p($start, $end, $calls); } //var_dump($tmp); echo 'DateTime::createFromTimestamp($t): ' . "\n"; $start = hrtime(true); for ($i = 0; $i < $calls; $i += 1) { $tmp = DateTime::createFromTimestamp($t); } $end = hrtime(true); p($start, $end, $calls); //var_dump($tmp); } ``` ``` $t = 1697016217 new DateTime("@" . $t, $utc): 0.012929698s for 10000 calls (1292.969800ns per call) DateTime::createFromFormat("U", $t, $utc): 0.009363419s for 10000 calls (936.341900ns per call) DateTime::createFromTimestamp($t): 0.004168669s for 10000 calls (416.866900ns per call) $t = 1697016217.492358 new DateTime("@" . $t, $utc): 0.013072768s for 10000 calls (1307.276800ns per call) DateTime::createFromFormat("U.u", $t, $utc): 0.012962979s for 10000 calls (1296.297900ns per call) DateTime::createFromTimestamp($t): 0.00396879s for 10000 calls (396.879000ns per call) ```

« previous php.git-pulls (#23859) next »