Skip to content

Commit

Permalink
Use finest overflow detection
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Feb 11, 2021
1 parent e2ba317 commit b65deb7
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/Carbon/Traits/Rounding.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function roundUnit($unit, $precision = 1, $function = 'round')
'microsecond' => [0, 999999],
]);
$factor = 1;
$initialMonth = $this->month;

if ($normalizedUnit === 'week') {
$normalizedUnit = 'day';
Expand Down Expand Up @@ -115,7 +116,7 @@ public function roundUnit($unit, $precision = 1, $function = 'round')
$result = $result->$unit($value);
}

return $normalizedUnit === 'month'
return $normalizedUnit === 'month' && $precision <= 1 && abs($result->month - $initialMonth) === 2
// Re-run the change in case an overflow occurred
? $result->$normalizedUnit($normalizedValue)
: $result;
Expand Down
4 changes: 3 additions & 1 deletion tests/Carbon/RoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,11 @@ public function testRoundWeek()
Carbon::setWeekEndsAt(Carbon::SUNDAY);
}

public function testRoundMonth()
public function testCeilMonth()
{
$this->assertCarbon(Carbon::parse('2021-01-29')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-01-31')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-12-17')->ceilMonth(), 2022, 1, 1, 0, 0, 0);
}

public function testRoundInvalidArgument()
Expand Down
4 changes: 3 additions & 1 deletion tests/CarbonImmutable/RoundTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,11 @@ public function testRoundWeek()
Carbon::setWeekEndsAt(Carbon::SUNDAY);
}

public function testRoundMonth()
public function testCeilMonth()
{
$this->assertCarbon(Carbon::parse('2021-01-29')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-01-31')->ceilMonth(), 2021, 2, 1, 0, 0, 0);
$this->assertCarbon(Carbon::parse('2021-12-17')->ceilMonth(), 2022, 1, 1, 0, 0, 0);
}

public function testRoundInvalidArgument()
Expand Down

0 comments on commit b65deb7

Please sign in to comment.