Carry a numeric rounding precision out of any unit, not just seconds
This commit is contained in:
1 parent
5192615f4e
commit
95c416685a
2 files changed
+51
-14
No files matched your search
@@ -237,4 +237,27 @@ class RoundTest extends AbstractTestCase
|
||||
// Sub-minute numeric precision must keep rounding within the second field, unchanged.
|
||||
$this->assertCarbon($dt->copy()->round(30), 2026, 1, 1, 14, 21, 30, 0);
|
||||
}
|
||||
|
||||
public function testNumericPrecisionCarryFromAnyUnit(): void
|
||||
{
|
||||
$dt = Carbon::parse('2026-01-01 14:21:03');
|
||||
|
||||
// A precision counting minutes spans a whole day just as one counting
|
||||
// seconds does, so it has to carry the same way.
|
||||
$this->assertCarbon($dt->copy()->roundMinutes(24 * 60), 2026, 1, 2, 0, 0, 0, 0);
|
||||
$this->assertTrue($dt->copy()->roundMinutes(24 * 60)->eq($dt->copy()->round('day')));
|
||||
|
||||
$this->assertCarbon($dt->copy()->roundHours(24), 2026, 1, 2, 0, 0, 0, 0);
|
||||
$this->assertTrue($dt->copy()->roundHours(24)->eq($dt->copy()->round('day')));
|
||||
|
||||
$this->assertCarbon($dt->copy()->roundMinutes(60), 2026, 1, 1, 14, 0, 0, 0);
|
||||
$this->assertTrue($dt->copy()->roundMinutes(60)->eq($dt->copy()->round('hour')));
|
||||
|
||||
$this->assertCarbon(Carbon::parse('2026-08-20')->roundMonths(12), 2027, 1, 1, 0, 0, 0, 0);
|
||||
$this->assertTrue(Carbon::parse('2026-08-20')->roundMonths(12)->eq(Carbon::parse('2026-08-20')->round('year')));
|
||||
|
||||
// A precision that stays inside its own unit is untouched.
|
||||
$this->assertCarbon($dt->copy()->roundMinutes(30), 2026, 1, 1, 14, 30, 0, 0);
|
||||
$this->assertCarbon($dt->copy()->roundSeconds(30), 2026, 1, 1, 14, 21, 0, 0);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user