You are here

function asset_search_parse_w3cdtf in Asset 6

Same name and namespace in other branches
  1. 5.2 contrib/asset_search/asset_search.parser.inc \asset_search_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 -1 if not.

1 call to asset_search_parse_w3cdtf()
asset_search_parse_feed in contrib/asset_search/asset_search.parser.inc
Parse an rss feed and return an array of items Taken from aggregator_parse_feed

File

contrib/asset_search/asset_search.parser.inc, line 137
Include file to parse RSS feeds into an array of items that will then be used as pseudo-assets.

Code

function asset_search_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;
  }
}