diff options
Diffstat (limited to 'src/Monolog/DateTimeImmutable.php')
-rw-r--r-- | src/Monolog/DateTimeImmutable.php | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/src/Monolog/DateTimeImmutable.php b/src/Monolog/DateTimeImmutable.php index 6ca786c..45b80c7 100644 --- a/src/Monolog/DateTimeImmutable.php +++ b/src/Monolog/DateTimeImmutable.php @@ -23,17 +23,27 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable public function __construct($useMicroseconds, \DateTimeZone $timezone = null) { + $this->useMicroseconds = $useMicroseconds; $date = 'now'; + if ($useMicroseconds) { + $timestamp = microtime(true); + + // apply offset of the timezone as microtime() is always UTC + if ($timezone && $timezone->getName() !== 'UTC') { + $timestamp += (new \DateTime('now', $timezone))->getOffset(); + } + // Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static` // @link https://bugs.php.net/bug.php?id=60302 - $timestamp = microtime(true); - $microseconds = sprintf("%06d", ($timestamp - floor($timestamp)) * 1000000); - $date = date('Y-m-d H:i:s.' . $microseconds, (int) $timestamp); + // + // So we create a DateTime but then format it so we + // can re-create one using the right class + $dt = self::createFromFormat('U.u', sprintf('%.6F', $timestamp)); + $date = $dt->format('Y-m-d H:i:s.u'); } - parent::__construct($date, $timezone); - $this->useMicroseconds = $useMicroseconds; + parent::__construct($date, $timezone); } public function jsonSerialize(): string |