Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop using start/end on interval if they are changed after creation #2981

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,15 @@ class CarbonInterval extends DateInterval implements CarbonConverterInterface
*/
protected ?CarbonInterface $endDate = null;

protected ?array $initialValues = null;

/**
* Set the instance's timezone from a string or object.
*/
public function setTimezone(DateTimeZone|string|int $timezone): static
{
$this->timezoneSetting = $timezone;
$this->checkStartAndEnd();

if ($this->startDate) {
$this->startDate = $this->startDate
Expand All @@ -316,6 +319,7 @@ public function setTimezone(DateTimeZone|string|int $timezone): static
public function shiftTimezone(DateTimeZone|string|int $timezone): static
{
$this->timezoneSetting = $timezone;
$this->checkStartAndEnd();

if ($this->startDate) {
$this->startDate = $this->startDate
Expand Down Expand Up @@ -749,6 +753,8 @@ public function original()
*/
public function start(): ?CarbonInterface
{
$this->checkStartAndEnd();

return $this->startDate;
}

Expand All @@ -759,6 +765,8 @@ public function start(): ?CarbonInterface
*/
public function end(): ?CarbonInterface
{
$this->checkStartAndEnd();

return $this->endDate;
}

Expand Down Expand Up @@ -1086,6 +1094,7 @@ public static function diff($start, $end = null, bool $absolute = false, array $

$interval->startDate = $start;
$interval->endDate = $end;
$interval->initialValues = $interval->getInnerValues();

return $interval;
}
Expand Down Expand Up @@ -2101,6 +2110,7 @@ public function toPeriod(...$params): CarbonPeriod
*/
public function stepBy($interval, Unit|string|null $unit = null): CarbonPeriod
{
$this->checkStartAndEnd();
$start = $this->startDate ?? CarbonImmutable::make('now');
$end = $this->endDate ?? $start->copy()->add($this);

Expand Down Expand Up @@ -2537,6 +2547,8 @@ public function total(string $unit): float
throw new UnknownUnitException($unit);
}

$this->checkStartAndEnd();

if ($this->startDate && $this->endDate) {
return $this->startDate->diffInUnit($unit, $this->endDate);
}
Expand Down Expand Up @@ -3237,4 +3249,21 @@ private function handleDecimalPart(string $unit, mixed $value, mixed $integerVal
$this->add($unit, $floatValue - $base);
}
}

private function getInnerValues(): array
{
return [$this->y, $this->m, $this->d, $this->h, $this->i, $this->s, $this->f, $this->invert, $this->days];
}

private function checkStartAndEnd(): void
{
if (
$this->initialValues !== null
&& ($this->startDate !== null || $this->endDate !== null)
&& $this->initialValues !== $this->getInnerValues()
) {
$this->startDate = null;
$this->endDate = null;
}
}
}
11 changes: 11 additions & 0 deletions tests/CarbonInterval/TotalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -280,4 +280,15 @@ public function testWithDiffInterval()
{
$this->assertSame(51.0, Carbon::parse('2020-08-10')->diff('2020-09-30')->totalDays);
}

public function testAlterationAfterDiff()
{
$t1 = Carbon::now();
$t2 = $t1->copy()->addMinutes(2);

$p = $t1->diffAsCarbonInterval($t2);
$p->addSeconds(10);

$this->assertSame(130.0, $p->totalSeconds);
}
}
Loading