mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-09 14:46:46 +02:00
Fixes microsecond timezone support, fixes #832
This commit is contained in:
@@ -28,9 +28,16 @@ class DateTimeImmutable extends \DateTimeImmutable implements \JsonSerializable
|
|||||||
// Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static`
|
// Circumvent DateTimeImmutable::createFromFormat() which always returns \DateTimeImmutable instead of `static`
|
||||||
// @link https://bugs.php.net/bug.php?id=60302
|
// @link https://bugs.php.net/bug.php?id=60302
|
||||||
$timestamp = microtime(true);
|
$timestamp = microtime(true);
|
||||||
$microseconds = sprintf("%06d", ($timestamp - floor($timestamp)) * 1000000);
|
|
||||||
$date = date('Y-m-d H:i:s.' . $microseconds, (int) $timestamp);
|
// apply offset of the timezone as microtime() is always UTC
|
||||||
|
if ($timezone && $timezone->getName() !== 'UTC') {
|
||||||
|
$timestamp += (new \DateTime('now', $timezone))->getOffset();
|
||||||
|
}
|
||||||
|
|
||||||
|
$dt = self::createFromFormat('U.u', sprintf('%.6F', $timestamp));
|
||||||
|
$date = $dt->format('Y-m-d H:i:s.u');
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::__construct($date, $timezone);
|
parent::__construct($date, $timezone);
|
||||||
|
|
||||||
$this->useMicroseconds = $useMicroseconds;
|
$this->useMicroseconds = $useMicroseconds;
|
||||||
|
@@ -494,6 +494,56 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Monolog\Logger::setTimezone
|
||||||
|
* @covers Monolog\DateTimeImmutable::__construct
|
||||||
|
*/
|
||||||
|
public function testTimezoneIsRespectedInUTC()
|
||||||
|
{
|
||||||
|
foreach ([true, false] as $microseconds) {
|
||||||
|
$logger = new Logger('foo');
|
||||||
|
$logger->useMicrosecondTimestamps($microseconds);
|
||||||
|
$tz = new \DateTimeZone('America/New_York');
|
||||||
|
$logger->setTimezone($tz);
|
||||||
|
$handler = new TestHandler;
|
||||||
|
$logger->pushHandler($handler);
|
||||||
|
$dt = new \DateTime('now', $tz);
|
||||||
|
$logger->info('test');
|
||||||
|
list($record) = $handler->getRecords();
|
||||||
|
|
||||||
|
$this->assertEquals($tz, $record['datetime']->getTimezone());
|
||||||
|
$this->assertEquals($dt->format('Y/m/d H:i'), $record['datetime']->format('Y/m/d H:i'), 'Time should match timezone with microseconds set to: '.var_export($microseconds, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Monolog\Logger::setTimezone
|
||||||
|
* @covers Monolog\DateTimeImmutable::__construct
|
||||||
|
*/
|
||||||
|
public function testTimezoneIsRespectedInOtherTimezone()
|
||||||
|
{
|
||||||
|
date_default_timezone_set('CET');
|
||||||
|
foreach ([true, false] as $microseconds) {
|
||||||
|
$logger = new Logger('foo');
|
||||||
|
$logger->useMicrosecondTimestamps($microseconds);
|
||||||
|
$tz = new \DateTimeZone('America/New_York');
|
||||||
|
$logger->setTimezone($tz);
|
||||||
|
$handler = new TestHandler;
|
||||||
|
$logger->pushHandler($handler);
|
||||||
|
$dt = new \DateTime('now', $tz);
|
||||||
|
$logger->info('test');
|
||||||
|
list($record) = $handler->getRecords();
|
||||||
|
|
||||||
|
$this->assertEquals($tz, $record['datetime']->getTimezone());
|
||||||
|
$this->assertEquals($dt->format('Y/m/d H:i'), $record['datetime']->format('Y/m/d H:i'), 'Time should match timezone with microseconds set to: '.var_export($microseconds, true));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function tearDown()
|
||||||
|
{
|
||||||
|
date_default_timezone_set('UTC');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider useMicrosecondTimestampsProvider
|
* @dataProvider useMicrosecondTimestampsProvider
|
||||||
* @covers Monolog\Logger::useMicrosecondTimestamps
|
* @covers Monolog\Logger::useMicrosecondTimestamps
|
||||||
|
Reference in New Issue
Block a user