mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-09-11 18:46:49 +00:00
Merge pull request #4150 from marc-mabe/fix-Date-roundMicroseconds
Fix Date::roundMicroseconds() not resetting microsecond part
This commit is contained in:
@@ -537,11 +537,16 @@ class Date
|
||||
return $dtobj->format($format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Round the given DateTime object to seconds.
|
||||
*/
|
||||
public static function roundMicroseconds(DateTime $dti): void
|
||||
{
|
||||
$microseconds = (int) $dti->format('u');
|
||||
if ($microseconds >= 500000) {
|
||||
$dti->modify('+1 second');
|
||||
$rounded = (int) round($microseconds, -6);
|
||||
$modify = $rounded - $microseconds;
|
||||
if ($modify !== 0) {
|
||||
$dti->modify(($modify > 0 ? '+' : '') . $modify . ' microseconds');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace PhpOffice\PhpSpreadsheetTests\Shared;
|
||||
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
use DateTimeZone;
|
||||
use PhpOffice\PhpSpreadsheet\Exception;
|
||||
@@ -249,4 +250,19 @@ class DateTest extends TestCase
|
||||
->setFormatCode('yyyy-mm-dd');
|
||||
self::assertFalse(Date::isDateTime($cella4));
|
||||
}
|
||||
|
||||
public function testRoundMicroseconds(): void
|
||||
{
|
||||
$dti = new DateTime('2000-01-02 03:04:05.999999');
|
||||
Date::roundMicroseconds($dti);
|
||||
self::assertEquals(new DateTime('2000-01-02 03:04:06.000000'), $dti);
|
||||
|
||||
$dti = new DateTime('2000-01-02 03:04:05.500000');
|
||||
Date::roundMicroseconds($dti);
|
||||
self::assertEquals(new DateTime('2000-01-02 03:04:06.000000'), $dti);
|
||||
|
||||
$dti = new DateTime('2000-01-02 03:04:05.499999');
|
||||
Date::roundMicroseconds($dti);
|
||||
self::assertEquals(new DateTime('2000-01-02 03:04:05.000000'), $dti);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user