1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog; 13 14use DateTimeZone; 15 16/** 17 * Overrides default json encoding of date time objects 18 * 19 * @author Menno Holtkamp 20 * @author Jordi Boggiano <j.boggiano@seld.be> 21 */ 22class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable 23{ 24 /** 25 * @var bool 26 */ 27 private $useMicroseconds; 28 29 public function __construct(bool $useMicroseconds, ?DateTimeZone $timezone = null) 30 { 31 $this->useMicroseconds = $useMicroseconds; 32 33 parent::__construct('now', $timezone); 34 } 35 36 public function jsonSerialize(): string 37 { 38 if ($this->useMicroseconds) { 39 return $this->format('Y-m-d\TH:i:s.uP'); 40 } 41 42 return $this->format('Y-m-d\TH:i:sP'); 43 } 44 45 public function __toString(): string 46 { 47 return $this->jsonSerialize(); 48 } 49} 50