Skip to content

Commit

Permalink
Merge pull request #3103 from briannesbitt/fix/issue-3102-setUnitNoOv…
Browse files Browse the repository at this point in the history
…erflow-immutable

Fix immutable return for setUnitNoOverflow
  • Loading branch information
kylekatarnls authored Nov 7, 2024
2 parents d481d8d + 9d1cc38 commit e1268cd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Carbon/Traits/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ private static function floorZeroPad(int|float $value, int $length): string
private function mutateIfMutable(CarbonInterface $date): CarbonInterface
{
return $this instanceof DateTimeImmutable
? $this
? $date
: $this->modify('@'.$date->rawFormat('U.u'))->setTimezone($date->getTimezone());
}
}
8 changes: 8 additions & 0 deletions tests/Carbon/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,14 @@ public function testMidDayAtSetter()
$this->assertSame(12, $d->getMidDayAt());
}

public function testSetUnitNoOverflowFebruary()
{
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');

$this->assertInstanceOf(Carbon::class, $d);
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
}

public function testSetUnitNoOverflow()
{
$results = [
Expand Down
8 changes: 8 additions & 0 deletions tests/CarbonImmutable/SettersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,12 @@ public function testSetter()
$d->setMidDayAt(12);
$this->assertSame(12, $d->getMidDayAt());
}

public function testSetUnitNoOverflowFebruary()
{
$d = Carbon::parse('2024-02-29')->setUnitNoOverFlow('day', 31, 'month');

$this->assertInstanceOf(Carbon::class, $d);
$this->assertSame('2024-02-29 23:59:59.999999', $d->format('Y-m-d H:i:s.u'));
}
}

0 comments on commit e1268cd

Please sign in to comment.