You are here

function date_ical_parse_duration in Date 7

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

Parse the duration of the event. Example: DURATION:PT1H30M DURATION:P1Y2M

Parameters

$subgroup: array of other values in the vevent so we can check for DTSTART

1 call to date_ical_parse_duration()
date_ical_parse in date_api/date_api_ical.inc
Return an array of iCalendar information from an iCalendar file.

File

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

Code

function date_ical_parse_duration(&$subgroup, $field = 'DURATION') {
  $items = $subgroup[$field];
  $data = $items['DATA'];
  preg_match('/^P(\\d{1,4}[Y])?(\\d{1,2}[M])?(\\d{1,2}[W])?(\\d{1,2}[D])?([T]{0,1})?(\\d{1,2}[H])?(\\d{1,2}[M])?(\\d{1,2}[S])?/', $data, $duration);
  $items['year'] = isset($duration[1]) ? str_replace('Y', '', $duration[1]) : '';
  $items['month'] = isset($duration[2]) ? str_replace('M', '', $duration[2]) : '';
  $items['week'] = isset($duration[3]) ? str_replace('W', '', $duration[3]) : '';
  $items['day'] = isset($duration[4]) ? str_replace('D', '', $duration[4]) : '';
  $items['hour'] = isset($duration[6]) ? str_replace('H', '', $duration[6]) : '';
  $items['minute'] = isset($duration[7]) ? str_replace('M', '', $duration[7]) : '';
  $items['second'] = isset($duration[8]) ? str_replace('S', '', $duration[8]) : '';
  $start_date = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['datetime'] : date_format(date_now(), DATE_FORMAT_ISO);
  $timezone = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['tz'] : variable_get('date_default_timezone');
  if (empty($timezone)) {
    $timezone = 'UTC';
  }
  $date = new DateObject($start_date, $timezone);
  $date2 = clone $date;
  foreach ($items as $item => $count) {
    if ($count > 0) {
      date_modify($date2, '+' . $count . ' ' . $item);
    }
  }
  $format = isset($subgroup['DTSTART']['type']) && $subgroup['DTSTART']['type'] == 'DATE' ? 'Y-m-d' : 'Y-m-d H:i:s';
  $subgroup['DTEND'] = array(
    'datetime' => date_format($date2, DATE_FORMAT_DATETIME),
    'all_day' => isset($subgroup['DTSTART']['all_day']) ? $subgroup['DTSTART']['all_day'] : 0,
    'tz' => $timezone,
  );
  $duration = date_format($date2, 'U') - date_format($date, 'U');
  $subgroup['DURATION'] = array(
    'DATA' => $data,
    'DURATION' => $duration,
  );
}