You are here

function date_ical_parse_duration in Date 5

Same name and namespace in other branches
  1. 5.2 date_api_ical.inc \date_ical_parse_duration()
  2. 6.2 date_api_ical.inc \date_ical_parse_duration()
  3. 6 date_api_ical.inc \date_ical_parse_duration()
  4. 7.3 date_api/date_api_ical.inc \date_ical_parse_duration()
  5. 7 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 and add DTEND.

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

File

./date_api_ical.inc, line 440
Parse iCal imports and create iCal exports. This file must be included when these functions are needed.

Code

function date_ical_parse_duration(&$subgroup) {
  $items = $subgroup['DURATION'];
  $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'] = str_replace('Y', '', $duration[1]);
  $items['month'] = str_replace('M', '', $duration[2]);
  $items['week'] = str_replace('W', '', $duration[3]);
  $items['day'] = str_replace('D', '', $duration[4]);
  $items['hour'] = str_replace('H', '', $duration[6]);
  $items['minute'] = str_replace('M', '', $duration[7]);
  $items['second'] = str_replace('S', '', $duration[8]);
  $start_date = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['datetime'] : date_unix2iso(date_time());
  $timezone = array_key_exists('DTSTART', $subgroup) ? $subgroup['DTSTART']['tz'] : variable_get('date_default_timezone_name', 'UTC');
  $date = date_make_date(str_replace(' ', 'T', $start_date), $timezone, 'local');
  $stamp1 = $date->local->timestamp;
  $stamp2 = $stamp1;
  foreach ($items as $item => $count) {
    if ($count > 0) {
      $stamp2 = strtotime('+' . $count . ' ' . $item, $stamp2);
    }
  }
  $format = date_limit_format('Y-m-d H:i:s', $subgroup['DTSTART']['granularity']);
  $subgroup['DTEND'] = array(
    'datetime' => date_format_date($format, $stamp2, NULL, $timezone),
    'all_day' => $subgroup['DTSTART']['all_day'],
    'tz' => $timezone,
  );
  $items['DURATION'] = $stamp2 - $stamp1;
  $subgroup['DURATION'] = $items;
}