You are here

function date_ical_parse_date in Date 7.3

Same name and namespace in other branches
  1. 5.2 date_api_ical.inc \date_ical_parse_date()
  2. 5 date_api_ical.inc \date_ical_parse_date()
  3. 6.2 date_api_ical.inc \date_ical_parse_date()
  4. 6 date_api_ical.inc \date_ical_parse_date()
  5. 7 date_api/date_api_ical.inc \date_ical_parse_date()
  6. 7.2 date_api/date_api_ical.inc \date_ical_parse_date()

Parses a ical date element.

Possible formats to parse include:

  • PROPERTY:YYYYMMDD[T][HH][MM][SS][Z]
  • PROPERTY;VALUE=DATE:YYYYMMDD[T][HH][MM][SS][Z]
  • PROPERTY;VALUE=DATE-TIME:YYYYMMDD[T][HH][MM][SS][Z]
  • PROPERTY;TZID=XXXXXXXX;VALUE=DATE:YYYYMMDD[T][HH][MM][SS]
  • PROPERTY;TZID=XXXXXXXX:YYYYMMDD[T][HH][MM][SS]

The property and the colon before the date are removed in the import process above and we are left with $field and $data.

@todo Another option for dates is the format PROPERTY;VALUE=PERIOD:XXXX. The period may include a duration, or a date and a duration, or two dates, so would have to be split into parts and run through date_ical_parse_date() and date_ical_parse_duration(). This is not commonly used, so ignored for now. It will take more work to figure how to support that.

Parameters

string $field: The text before the colon and the date, i.e. ';VALUE=DATE:', ';VALUE=DATE-TIME:', ';TZID='

string $data: The date itself, after the colon, in the format YYYYMMDD[T][HH][MM][SS][Z] 'Z', if supplied, means the date is in UTC.

Return value

array Consisting of: 'datetime' => Date in YYYY-MM-DD HH:MM format, not timezone adjusted. 'all_day' => Whether this is an all-day event with no time. 'tz' => The timezone of the date, could be blank if the iCal has no timezone; the ical specs say no timezone conversion should be done if no timezone info is supplied.

3 calls to date_ical_parse_date()
date_ical_parse in date_api/date_api_ical.inc
Returns an array of iCalendar information from an iCalendar file.
date_ical_parse_exceptions in date_api/date_api_ical.inc
Parse exception dates (can be multiple values).
date_ical_parse_rrule in date_api/date_api_ical.inc
Parse an ical repeat rule.

File

date_api/date_api_ical.inc, line 369
Parse iCal data.

Code

function date_ical_parse_date($field, $data) {
  $items = array(
    'datetime' => '',
    'all_day' => '',
    'tz' => '',
  );
  if (empty($data)) {
    return $items;
  }

  // Make this a little more whitespace independent.
  $data = trim($data);

  // Turn the properties into a nice indexed array of
  // @code
  // array(PROPERTYNAME => PROPERTYVALUE);
  // @endcode
  $field_parts = preg_split('/[;:]/', $field);
  $properties = array();
  foreach ($field_parts as $part) {
    if (strpos($part, '=') !== FALSE) {
      $tmp = explode('=', $part);
      $properties[$tmp[0]] = $tmp[1];
    }
  }

  // Make this a little more whitespace independent.
  $data = trim($data);

  // Record if a time has been found.
  $has_time = FALSE;

  // If a format is specified, parse it according to that format.
  if (isset($properties['VALUE'])) {
    switch ($properties['VALUE']) {
      case 'DATE':
        preg_match(DATE_REGEX_ICAL_DATE, $data, $regs);

        // Date.
        $datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);
        break;
      case 'DATE-TIME':
        preg_match(DATE_REGEX_ICAL_DATETIME, $data, $regs);

        // Date.
        $datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);

        // Time.
        $datetime .= ' ' . date_pad($regs[4]) . ':' . date_pad($regs[5]) . ':' . date_pad($regs[6]);
        $has_time = TRUE;
        break;
    }
  }
  else {
    preg_match(DATE_REGEX_LOOSE, $data, $regs);
    if (!empty($regs) && count($regs) > 2) {

      // Date.
      $datetime = date_pad($regs[1]) . '-' . date_pad($regs[2]) . '-' . date_pad($regs[3]);
      if (isset($regs[4])) {
        $has_time = TRUE;

        // Time.
        $datetime .= ' ' . (!empty($regs[5]) ? date_pad($regs[5]) : '00') . ':' . (!empty($regs[6]) ? date_pad($regs[6]) : '00') . ':' . (!empty($regs[7]) ? date_pad($regs[7]) : '00');
      }
    }
  }

  // Use timezone if explicitly declared.
  if (isset($properties['TZID'])) {
    $tz = $properties['TZID'];

    // Fix alternatives like US-Eastern which should be US/Eastern.
    $tz = str_replace('-', '/', $tz);

    // Unset invalid timezone names.
    module_load_include('inc', 'date_api', 'date_api.admin');
    $tz = _date_timezone_replacement($tz);
    if (!date_timezone_is_valid($tz)) {
      $tz = '';
    }
  }
  elseif (strpos($data, 'Z') !== FALSE) {
    $tz = 'UTC';
  }
  else {
    $tz = '';
  }
  $items['datetime'] = $datetime;
  $items['all_day'] = $has_time ? FALSE : TRUE;
  $items['tz'] = $tz;
  return $items;
}