Skip to content
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

Handle suffixed time zone names in date parsing #220

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 15 additions & 1 deletion src/Fetch/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions tests/Fetch/Test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down