The aggregator.module in HEAD currently only supports iso8601 dates in the format of "yyyy-mm-ddThh:mm:ss". Unfortunately, iso8601 dates can be far more generic or detailed then that. Nor does it handle timezones. I've attached a patch that makes it support a /few more/ iso8601 dates in the wild, but certainly not all the possibilities. Likewise, drumm suggests that instead of using strtotime, we use gmmktime, adding/subtracting the item timezone after the fact:

print strtotime("2004-06-15 07:14:00-0500") . "\n";
print gmmktime(07, 14, 00, 06, 15, 2004) - (60 * 60 * 5);

I agree with this approach, but didn't have time to properly modify the patch to support it (as well as timezones that include half-hours), etc., etc.

CommentFileSizeAuthor
#6 aggregator.w3cdtf.diff4.71 KBdrumm
aggro.patch1.67 KBmorbus iff

Comments

mda-1’s picture

Here is my current fix (mostly comments). This is based on painful examination of
the retarded grammar that php uses.

// first see if strtotime manages it.
$timestamp = strtotime($date); // strtotime() returns -1 on failure

if ($timestamp < 0) {
/*
* You might think from the php docs at http://us4.php.net/strtotime
* that php strtotime uses gnu strtotime. It doesn't.
* Rather, it implements its own grammar at:
* http://cvs.php.net/php-src/ext/standard/parsedate.y
* In http://cvs.php.net/php-src/ext/standard/
* As you can see there, the support for parsing ISO8601 dates is poor prior to v1.52 of Mar 1 05:42:27 2004
*
* As an example feed that has dates with timezone offsets, see
* http://www.rpgdot.com/team/rss/rss13.xml
* For example, it has dc:date of "2004-06-15T09:55+06:00".
*/
// remove numerical timezone, and parse that ourselves
if (preg_match('/(.*)([\+\-]\d\d):?(\d\d)/', $date, $matches)) {
$timestamp = strtotime($matches[1]);
$secs = 0 + $matches[2] * 3600 + $matches[3] * 60;
//print("timestamp=$timestamp secs=$secs from ($matches[1],$matches[2],$matches[3]) in $date\n");
// timezone offsets are local time minus UTC. so UTC = localtime - offset.
$timestamp -= $secs;
}
else {
//print("date $date did not match my tz pattern\n");
}
}

morbus iff’s picture

See also rss_date_utime in import.module.

mda-1’s picture

Once again, wrapped with php tags....

    // first see if strtotime manages it.
    $timestamp = strtotime($date);  // strtotime() returns -1 on failure

    if ($timestamp < 0) {
        /*
         * You might think from the php docs at http://us4.php.net/strtotime
         * that php strtotime uses gnu strtotime. It doesn't.
         * Rather, it implements its own grammar at:
         *    http://cvs.php.net/php-src/ext/standard/parsedate.y
         * In http://cvs.php.net/php-src/ext/standard/
         * As you can see there, the support for parsing ISO8601 dates is poor prior to v1.52 of Mar 1 05:42:27 2004
         *
         * As an example feed that has dates with timezone offsets, see
         *    http://www.rpgdot.com/team/rss/rss13.xml
         * For example, it has dc:date of "2004-06-15T09:55+06:00".
         */
        // remove numerical timezone, and parse that ourselves
        if (preg_match('/(.*)([\+\-]\d\d):?(\d\d)/', $date, $matches)) {
            $timestamp = strtotime($matches[1]);
            $secs = 0 + $matches[2] * 3600 + $matches[3] * 60;
            //print("timestamp=$timestamp secs=$secs from ($matches[1],$matches[2],$matches[3]) in $date\n");
            // timezone offsets are local time minus UTC. so UTC = localtime - offset.
            $timestamp -= $secs;
        }
        else {
            //print("date $date did not match my tz pattern\n");
        }
    }

mda-1’s picture

A third time, wrapped with pre tags....

    // first see if strtotime manages it.
    $timestamp = strtotime($date);  // strtotime() returns -1 on failure

    if ($timestamp < 0) {
        /*
         * You might think from the php docs at http://us4.php.net/strtotime
         * that php strtotime uses gnu strtotime. It doesn't.
         * Rather, it implements its own grammar at:
         *    http://cvs.php.net/php-src/ext/standard/parsedate.y
         * In http://cvs.php.net/php-src/ext/standard/
         * As you can see there, the support for parsing ISO8601 dates is poor prior to v1.52 of Mar 1 05:42:27 2004
         *
         * As an example feed that has dates with timezone offsets, see
         *    http://www.rpgdot.com/team/rss/rss13.xml
         * For example, it has dc:date of "2004-06-15T09:55+06:00".
         */
        // remove numerical timezone, and parse that ourselves
        if (preg_match('/(.*)([\+\-]\d\d):?(\d\d)/', $date, $matches)) {
            $timestamp = strtotime($matches[1]);
            $secs = 0 + $matches[2] * 3600 + $matches[3] * 60;
            //print("timestamp=$timestamp secs=$secs from ($matches[1],$matches[2],$matches[3]) in $date\n");
            // timezone offsets are local time minus UTC. so UTC = localtime - offset.
            $timestamp -= $secs;
        }
        else {
            //print("date $date did not match my tz pattern\n");
        }
    }

mda-1’s picture

Ok, now with square brackets. Are well all fed up now?

    // first see if strtotime manages it.
    $timestamp = strtotime($date);  // strtotime() returns -1 on failure

    if ($timestamp < 0) {
        /*
         * You might think from the php docs at http://us4.php.net/strtotime
         * that php strtotime uses gnu strtotime. It doesn't.
         * Rather, it implements its own grammar at:
         *    http://cvs.php.net/php-src/ext/standard/parsedate.y
         * In http://cvs.php.net/php-src/ext/standard/
         * As you can see there, the support for parsing ISO8601 dates is poor prior to v1.52 of Mar 1 05:42:27 2004
         *
         * As an example feed that has dates with timezone offsets, see
         *    http://www.rpgdot.com/team/rss/rss13.xml
         * For example, it has dc:date of "2004-06-15T09:55+06:00".
         */
        // remove numerical timezone, and parse that ourselves
        if (preg_match('/(.*)([\+\-]\d\d):?(\d\d)/', $date, $matches)) {
            $timestamp = strtotime($matches[1]);
            $secs = 0 + $matches[2] * 3600 + $matches[3] * 60;
            //print("timestamp=$timestamp secs=$secs from ($matches[1],$matches[2],$matches[3]) in $date\n");
            // timezone offsets are local time minus UTC. so UTC = localtime - offset.
            $timestamp -= $secs;
        }
        else {
            //print("date $date did not match my tz pattern\n");
        }

drumm’s picture

Assigned: Unassigned » drumm
StatusFileSize
new4.71 KB

Took this date parser from MagpieRSS (conveniently GPL). The main benefit here is that timezones are handled properly (I have been noticing increasing complaints about that). I tested with a few feeds of different time formats and time zones and everything appeared correctly in my site's local time. Making this another function adds the benefit of parsability by other RSS (or other contexts) and RSS extension handling functions, where the W3C DTF is recomended.

drumm’s picture

And a couple code style updates made it in too.

dries’s picture

Committed to HEAD.

Anonymous’s picture