allow vendord
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m3s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 32s
Build, Push and Deploy / deploy-production (push) Has been skipped
Build, Push and Deploy / cleanup (push) Successful in 1s

This commit is contained in:
2026-03-30 14:54:57 +07:00
parent 66aed7c4e8
commit b5e3a778ce
21316 changed files with 2777892 additions and 13 deletions

View File

@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace Kreait\Firebase\Util;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use Kreait\Firebase\Exception\InvalidArgumentException;
use Throwable;
use function ctype_digit;
use function get_debug_type;
use function is_bool;
use function is_object;
use function is_scalar;
use function mb_strlen;
use function method_exists;
use function preg_match;
use function sprintf;
use function time;
/**
* @internal
*/
final class DT
{
public static function toUTCDateTimeImmutable(mixed $value): DateTimeImmutable
{
$tz = new DateTimeZone('UTC');
$now = time();
if ($value instanceof DateTimeInterface) {
$result = DateTimeImmutable::createFromFormat('U.u', $value->format('U.u'));
if ($result instanceof DateTimeImmutable) {
return $result->setTimezone($tz);
}
}
if ($value === null || $value === 0 || is_bool($value)) {
$value = '0';
}
if (is_scalar($value) || (is_object($value) && method_exists($value, '__toString'))) {
$value = (string) $value;
} else {
$type = get_debug_type($value);
throw new InvalidArgumentException("This {$type} cannot be parsed to a DateTime value");
}
if (ctype_digit($value)) {
// Seconds
if (($value === '0' || mb_strlen($value) === mb_strlen((string) $now))) {
$result = DateTimeImmutable::createFromFormat('U', $value);
if ($result instanceof DateTimeImmutable) {
return $result->setTimezone($tz);
}
}
// Milliseconds
if (mb_strlen($value) === mb_strlen((string) ($now * 1000))) {
$floatValue = (float) $value;
$result = DateTimeImmutable::createFromFormat('U.u', sprintf('%F', $floatValue / 1000));
if ($result !== false) {
return $result->setTimezone($tz);
}
}
}
// microtime
if (preg_match('@(?P<msec>^0?\.\d+) (?P<sec>\d+)$@', $value, $matches) === 1) {
$value = (string) ((float) $matches['sec'] + (float) $matches['msec']);
$result = DateTimeImmutable::createFromFormat('U.u', sprintf('%F', $value));
if ($result instanceof DateTimeImmutable) {
return $result->setTimezone($tz);
}
}
try {
return (new DateTimeImmutable($value))->setTimezone($tz);
} catch (Throwable $e) {
throw new InvalidArgumentException($e->getMessage());
}
}
}