You are here

function aggregator_parse_w3cdtf in Drupal 6

Same name and namespace in other branches
  1. 4 modules/aggregator.module \aggregator_parse_w3cdtf()
  2. 5 modules/aggregator/aggregator.module \aggregator_parse_w3cdtf()
  3. 7 modules/aggregator/aggregator.parser.inc \aggregator_parse_w3cdtf()

Parse the W3C date/time format, a subset of ISO 8601. PHP date parsing functions do not handle this format. See http://www.w3.org/TR/NOTE-datetime for more information. Originally from MagpieRSS (http://magpierss.sourceforge.net/).

Parameters

$date_str: A string with a potentially W3C DTF date.

Return value

A timestamp if parsed successfully or FALSE if not.

1 call to aggregator_parse_w3cdtf()
aggregator_parse_feed in modules/aggregator/aggregator.module
Parse a feed and store its items.

File

modules/aggregator/aggregator.module, line 661
Used to aggregate syndicated content (RSS, RDF, and Atom).

Code

function aggregator_parse_w3cdtf($date_str) {
  if (preg_match('/(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})(:(\\d{2}))?(?:([-+])(\\d{2}):?(\\d{2})|(Z))?/', $date_str, $match)) {
    list($year, $month, $day, $hours, $minutes, $seconds) = array(
      $match[1],
      $match[2],
      $match[3],
      $match[4],
      $match[5],
      $match[6],
    );

    // calc epoch for current date assuming GMT
    $epoch = gmmktime($hours, $minutes, $seconds, $month, $day, $year);
    if ($match[10] != 'Z') {

      // Z is zulu time, aka GMT
      list($tz_mod, $tz_hour, $tz_min) = array(
        $match[8],
        $match[9],
        $match[10],
      );

      // zero out the variables
      if (!$tz_hour) {
        $tz_hour = 0;
      }
      if (!$tz_min) {
        $tz_min = 0;
      }
      $offset_secs = ($tz_hour * 60 + $tz_min) * 60;

      // is timezone ahead of GMT?  then subtract offset
      if ($tz_mod == '+') {
        $offset_secs *= -1;
      }
      $epoch += $offset_secs;
    }
    return $epoch;
  }
  else {
    return FALSE;
  }
}