Update date formatting and add null-safe checks for pivot dates

Revised the date normalization format to align with ISO standards and ensure consistency. Additionally, introduced null-safe checks for pivot dates to avoid potential errors when these values are unset. Updated related tests to reflect these changes.
This commit is contained in:
Julien Fastré 2025-04-02 11:35:47 +02:00
parent 89aed74355
commit 10f66afdcd
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 7 additions and 6 deletions

View File

@ -63,7 +63,7 @@ class RollingDate
final public const T_YEAR_PREVIOUS_START = 'year_previous_start';
private const NORMALIZATION_FORMAT = 'd-m-Y-H:i:s.u T';
private const NORMALIZATION_FORMAT = 'Y-m-d-H:i:s.u e';
/**
* @param string|self::T_* $roll
@ -95,7 +95,7 @@ class RollingDate
return [
'roll' => $this->getRoll(),
'fixed_date' => $this->getFixedDate()?->format(self::NORMALIZATION_FORMAT),
'pivot_date' => $this->getPivotDate()->format(self::NORMALIZATION_FORMAT),
'pivot_date' => $this->getPivotDate()?->format(self::NORMALIZATION_FORMAT),
'v' => 1,
];
}

View File

@ -29,17 +29,18 @@ class RollingDateTest extends TestCase
self::assertEquals(RollingDate::T_YEAR_PREVIOUS_START, $actual->getRoll());
self::assertNull($actual->getFixedDate());
self::assertEquals($date->getPivotDate()->getTimestamp(), $actual->getPivotDate()->getTimestamp());
self::assertEquals($date->getPivotDate()?->getTimestamp(), $actual->getPivotDate()?->getTimestamp());
}
public function testNormalizationDenormalizationProcessWithPivotDate(): void
{
$date = new RollingDate(RollingDate::T_FIXED_DATE, $pivot = new \DateTimeImmutable('now'));
$date = new RollingDate(RollingDate::T_FIXED_DATE, $fixed = new \DateTimeImmutable('now'));
$actual = RollingDate::fromNormalized($date->normalize());
self::assertEquals(RollingDate::T_FIXED_DATE, $actual->getRoll());
self::assertEquals($pivot, $actual->getFixedDate());
self::assertEquals($date->getPivotDate()->getTimestamp(), $actual->getPivotDate()->getTimestamp());
self::assertEquals($fixed, $actual->getFixedDate());
self::assertEquals($date->getPivotDate()?->getTimestamp(), $actual->getPivotDate()?->getTimestamp());
}
}