zbateson / mail-mime-parser

An email parser written in PHP
https://mail-mime-parser.org/
BSD 2-Clause "Simplified" License
458 stars 58 forks source link

"UT" timezone is not correctly handled by PHP #97

Closed pupaxxo closed 4 years ago

pupaxxo commented 4 years ago

Hi,

PHP isn't fully supporting RFC 822 (https://bugs.php.net/bug.php?id=42486). Some (old) email severs (maybe due to some bugs) use "UT" timezone instead of "UTC". The difference is less than 1s (in the last 40 years, something between -0.6 and +0.8s).


// this returns false
\DateTime::createFromFormat(\DateTime::RFC822, 'Sun, 20 May 2012 23:08:52 UT') 

// if we add a 'C' at the end and parsing it as 2822 it returns the correct date
\DateTime::createFromFormat(\DateTime::RFC2822, 'Sun, 20 May 2012 23:08:52 UTC')

The fix could be as easy as adding a 'C' at then end:

# DatePart.php

// @see https://bugs.php.net/bug.php?id=42486
if ($date === false && preg_match('#UT$#', $dateToken)) {
     $date = DateTime::createFromFormat(DateTime::RFC2822, $dateToken . 'C');
}

We also noticed some errors while parsing years with only 2 digits, they can be easily fixed with something like this:


if ($date !== false) {
     if ($date->format('Y') < 50) {
           $date->setDate(intval($date->format('Y')) + 2000, $date->format('m'), $date->format('d'));
    } elseif ($date->format('Y') < 100) {
          $date->setDate(intval($date->format('Y')) + 1900, $date->format('m'), $date->format('d'));
    }
}

Does it make sense to create a pull request for fixing it?

zbateson commented 4 years ago

Lovely thanks for reporting, and yes please on a pull request :)