Improve RFC 2822 date parser due to PHP createFromFormat() shortcomings

https://github.com/the-djmaze/snappymail/issues/1694#issuecomment-2270983942
This commit is contained in:
the-djmaze 2024-08-06 12:50:24 +02:00
parent c0ed491f7a
commit 505e6e2a0d

View file

@ -42,7 +42,9 @@ abstract class DateTimeHelper
}
$sDateTime = \trim(\preg_replace('/ \([a-zA-Z0-9]+\)$/', '', $sDateTime));
$oDateTime = \DateTime::createFromFormat(\DateTime::RFC2822, $sDateTime, static::GetUtcTimeZoneObject());
$sDateTime = \preg_replace('/ ([0-9]+:[0-9]+) /', ' $1:00 ', $sDateTime);
$oDateTime = \DateTime::createFromFormat(\DateTime::RFC2822, $sDateTime, static::GetUtcTimeZoneObject())
?: \DateTime::createFromFormat('d M Y H:i:s O', $sDateTime, static::GetUtcTimeZoneObject());
// 398045302 is 1982-08-13 00:08:22 the date RFC 822 was created
if (!$oDateTime || 398045302 > $oDateTime->getTimestamp()) {
\SnappyMail\Log::notice('', "Failed to parse RFC 2822 date '{$sDateTime}'");