Skip to content

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

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 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public function transform($dateTime)
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 @@ -130,7 +131,7 @@ public function reverseTransform($value)
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()
// check for the absence of time-related placeholders
return 0 === preg_match('#[ahHkKmsSAzZOvVxX]#', $pattern);
}

private function getCalendarConvertingFormatter()
{
$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 @@ -1017,4 +1017,19 @@ public function provideEmptyData()
'Compound choice fields' => ['choice', ['year' => '2018', 'month' => '11', 'day' => '11'], $expectedData],
];
}

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());
}
}