You are here

public static function DateiCalParse::parse_date in Date 8

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.

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 $items 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 @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.

3 calls to DateiCalParse::parse_date()
DateiCalParse::parse in date_api/lib/Drupal/date_api/DateiCalParse.php
Returns an array of iCalendar information from an iCalendar file.
DateiCalParse::parse_exceptions in date_api/lib/Drupal/date_api/DateiCalParse.php
Parse exception dates (can be multiple values).
DateiCalParse::parse_rrule in date_api/lib/Drupal/date_api/DateiCalParse.php
Parse an ical repeat rule.

File

date_api/lib/Drupal/date_api/DateiCalParse.php, line 400
Parse iCal data.

Class

DateiCalParse
Return an array of iCalendar information from an iCalendar file.

Namespace

Drupal\date_api

Code

public static function parse_date($data, $field = 'DATE:') {
  $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
  // array(PROPERTYNAME => PROPERTYVALUE);
  $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(self::$regex_ical_date, $data, $regs);

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

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

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

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

        // Time.
        $datetime .= ' ' . (!empty($regs[5]) ? DrupalDateTime::datePad($regs[5]) : '00') . ':' . (!empty($regs[6]) ? DrupalDateTime::datePad($regs[6]) : '00') . ':' . (!empty($regs[7]) ? DrupalDateTime::datePad($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 (!in_array($tz, array_keys(system_time_zones()))) {
      $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;
}