Skip to content

Commit

Permalink
Fix comparison of DateTimeImmutable objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus authored and sebastianbergmann committed Jan 12, 2018
1 parent a5f3ff3 commit 52ff72c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/DateTimeComparator.php
Expand Up @@ -48,18 +48,18 @@ public function assertEquals($expected, $actual, $delta = 0.0, $canonicalize = f

$delta = new \DateInterval(sprintf('PT%dS', abs($delta)));

$actualClone = clone $actual;
$actualClone->setTimezone(new \DateTimeZone('UTC'));
$actualClone = (clone $actual)
->setTimezone(new \DateTimeZone('UTC'));

$expectedLower = clone $expected;
$expectedLower->setTimezone(new \DateTimeZone('UTC'));
$expectedLower->sub($delta);
$expectedLower = (clone $expected)
->setTimezone(new \DateTimeZone('UTC'))
->sub($delta);

$expectedUpper = clone $expected;
$expectedUpper->setTimezone(new \DateTimeZone('UTC'));
$expectedUpper->add($delta);
$expectedUpper = (clone $expected)
->setTimezone(new \DateTimeZone('UTC'))
->add($delta);

if ($actual < $expectedLower || $actual > $expectedUpper) {
if ($actualClone < $expectedLower || $actualClone > $expectedUpper) {
throw new ComparisonFailure(
$expected,
$actual,
Expand Down
7 changes: 6 additions & 1 deletion tests/DateTimeComparatorTest.php
Expand Up @@ -86,7 +86,12 @@ public function assertEqualsSucceedsProvider()
[
new DateTime('2013-03-29T05:13:35-0500'),
new DateTime('2013-03-29T04:13:35-0600')
]
],
[
new DateTimeImmutable('2013-03-30', new DateTimeZone('America/New_York')),
new DateTimeImmutable('2013-03-29 23:01:30', new DateTimeZone('America/Chicago')),
100
],
];
}

Expand Down

0 comments on commit 52ff72c

Please sign in to comment.