diff --git a/src/Fetch/Message.php b/src/Fetch/Message.php index e382678..c97c668 100755 --- a/src/Fetch/Message.php +++ b/src/Fetch/Message.php @@ -335,7 +335,7 @@ public function getHeaders($forceReload = false) // to keep this object as close as possible to the original header object we add the udate property if (isset($headerObject->date)) { - $headerObject->udate = strtotime($headerObject->date); + $headerObject->udate = self::parseDate($headerObject->date); } else { $headerObject->date = null; $headerObject->udate = null; @@ -364,6 +364,20 @@ public function getStructure($forceReload = false) return $this->structure; } + public static function parseDate($mailDate) + { + // Strip alphabetical timezone suffix like (W. Europe Daylight Time) or (GMT-07:00) + $sanitized = preg_replace('/ \(.+\)$/i', '', $mailDate); + + $result = strtotime($sanitized); + + if ($result === false) { + throw new \RuntimeException('Unable to parse date: ' . $mailDate); + } + + return $result; + } + /** * This function returns the message body of the email. By default it returns the plaintext version. If a plaintext * version is requested but not present, the html version is stripped of tags and returned. If the opposite occurs, diff --git a/tests/Fetch/Test/MessageTest.php b/tests/Fetch/Test/MessageTest.php index 0cb9a76..05a87a1 100644 --- a/tests/Fetch/Test/MessageTest.php +++ b/tests/Fetch/Test/MessageTest.php @@ -43,6 +43,13 @@ public function testGetOverview() $this->assertEquals(1, $overview->seen, 'Seen available from overview'); } + public function testDateParsing() + { + $this->assertEquals(1385966267, Message::parseDate('Sun, 1 Dec 2013 22:37:47 -0800')); + $this->assertEquals(1441333422, Message::parseDate('Thu, 3 Sep 2015 19:23:42 -0700 (GMT-07:00)')); + $this->assertEquals(1565789188, Message::parseDate('Wed, 14 Aug 2019 15:26:28 +0200 (W. Europe Daylight Time)')); + } + public function testGetHeaders() { $message = static::getMessage(3);