Skip to content

[Form] properly parse dates before the Gregorian calendar #54288

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

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install:
- echo max_execution_time=1200 >> php.ini-min
- echo post_max_size=2047M >> php.ini-min
- echo upload_max_filesize=2047M >> php.ini-min
- echo date.timezone="America/Los_Angeles" >> php.ini-min
- echo date.timezone="UTC" >> php.ini-min
- echo extension_dir=ext >> php.ini-min
- echo extension=php_xsl.dll >> php.ini-min
- copy /Y php.ini-min php.ini-max
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ public function transform(mixed $dateTime): string
throw new TransformationFailedException('Expected a \DateTimeInterface.');
}

$value = $this->getIntlDateFormatter()->format($dateTime->getTimestamp());
$timestamp = $this->getCalendarConvertingFormatter()->parse($dateTime->format('Y-m-d H:i:s'));
$value = $this->getIntlDateFormatter()->format($timestamp);

if (0 != intl_get_error_code()) {
throw new TransformationFailedException(intl_get_error_message());
Expand Down Expand Up @@ -134,7 +135,7 @@ public function reverseTransform(mixed $value): ?\DateTime
try {
if ($dateOnly) {
// we only care about year-month-date, which has been delivered as a timestamp pointing to UTC midnight
$dateTime = new \DateTime(gmdate('Y-m-d', $timestamp), new \DateTimeZone($this->outputTimezone));
$dateTime = new \DateTime($this->getCalendarConvertingFormatter()->format($timestamp), new \DateTimeZone($this->outputTimezone));
} else {
// read timestamp into DateTime object - the formatter delivers a timestamp
$dateTime = new \DateTime(sprintf('@%s', $timestamp));
Expand Down Expand Up @@ -196,4 +197,11 @@ protected function isPatternDateOnly(): bool
// check for the absence of time-related placeholders
return 0 === preg_match('#[ahHkKmsSAzZOvVxX]#', $pattern);
}

private function getCalendarConvertingFormatter(): \IntlDateFormatter
{
$timezone = $this->isPatternDateOnly() ? 'UTC' : $this->inputTimezone;

return new \IntlDateFormatter(\Locale::getDefault(), \IntlDateFormatter::MEDIUM, \IntlDateFormatter::NONE, new \DateTimeZone($timezone), \IntlDateFormatter::GREGORIAN, 'yyyy-MM-dd HH:mm:ss');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1160,4 +1160,19 @@ protected function getTestOptions(): array
{
return ['widget' => 'choice'];
}

public function testSubmitDateBeforeBeginningOfGregorianCalendar()
{
if (\PHP_INT_SIZE < 8) {
$this->markTestSkipped('Parsing three digits years requires a 64bit PHP.');
}

$form = $this->factory->create(static::TESTED_TYPE, null, [
'widget' => 'single_text',
]);
$form->submit('950-12-19');

$this->assertSame('0950-12-19', $form->getData()->format('Y-m-d'));
$this->assertSame('0950-12-19', $form->getViewData());
}
}