/** * Constructs an array of seconds. * * @param string $format * (optional) A date format string that indicates the format to use for the * seconds. Defaults to 's'. * @param bool $required * (optional) If FALSE, the returned array will include a blank value. * Defaults to FALSE. * @param int $increment * An integer value to increment the values. Defaults to 1. * * @return array * An array of seconds in the selected format. */ public static function seconds($format = 's', $required = FALSE, $increment = 1) { $seconds = array(); // Ensure $increment has a value so we don't loop endlessly. if (empty($increment)) { $increment = 1; } for ($i = 0; $i < 60; $i += $increment) { $formatted = $format == 's' ? DrupalDateTime::datePad($i) : $i; $seconds[$i] = $formatted; } $none = array('' => ''); return !$required ? $none + $seconds : $seconds; }
/** * 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. * * @param string $field * The text before the colon and the date, i.e. * ';VALUE=DATE:', ';VALUE=DATE-TIME:', ';TZID=' * @param 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 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. */ 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; }