3 namespace Sabre\VObject;
8 * This class is responsible for parsing the several different date and time
9 * formats iCalendar and vCards have.
11 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
12 * @author Evert Pot (http://www.rooftopsolutions.nl/)
13 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
15 class DateTimeParser {
18 * Parses an iCalendar (rfc5545) formatted datetime and returns a DateTime object
20 * Specifying a reference timezone is optional. It will only be used
21 * if the non-UTC format is used. The argument is used as a reference, the
22 * returned DateTime object will still be in the UTC timezone.
25 * @param DateTimeZone $tz
28 static public function parseDateTime($dt,\DateTimeZone $tz = null) {
30 // Format is YYYYMMDD + "T" + hhmmss
31 $result = preg_match('/^([1-3][0-9]{3})([0-1][0-9])([0-3][0-9])T([0-2][0-9])([0-5][0-9])([0-5][0-9])([Z]?)$/',$dt,$matches);
34 throw new \LogicException('The supplied iCalendar datetime value is incorrect: ' . $dt);
37 if ($matches[7]==='Z' || is_null($tz)) {
38 $tz = new \DateTimeZone('UTC');
40 $date = new \DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3] . ' ' . $matches[4] . ':' . $matches[5] .':' . $matches[6], $tz);
42 // Still resetting the timezone, to normalize everything to UTC
43 $date->setTimeZone(new \DateTimeZone('UTC'));
49 * Parses an iCalendar (rfc5545) formatted date and returns a DateTime object
54 static public function parseDate($date) {
57 $result = preg_match('/^([1-3][0-9]{3})([0-1][0-9])([0-3][0-9])$/',$date,$matches);
60 throw new \LogicException('The supplied iCalendar date value is incorrect: ' . $date);
63 $date = new \DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3], new \DateTimeZone('UTC'));
69 * Parses an iCalendar (RFC5545) formatted duration value.
71 * This method will either return a DateTimeInterval object, or a string
72 * suitable for strtotime or DateTime::modify.
74 * @param string $duration
75 * @param bool $asString
76 * @return DateInterval|string
78 static public function parseDuration($duration, $asString = false) {
80 $result = preg_match('/^(?P<plusminus>\+|-)?P((?P<week>\d+)W)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?$/', $duration, $matches);
82 throw new \LogicException('The supplied iCalendar duration value is incorrect: ' . $duration);
87 if ($matches['plusminus']==='-') {
99 foreach($parts as $part) {
100 $matches[$part] = isset($matches[$part])&&$matches[$part]?(int)$matches[$part]:0;
104 // We need to re-construct the $duration string, because weeks and
105 // days are not supported by DateInterval in the same string.
107 $days = $matches['day'];
108 if ($matches['week']) {
109 $days+=$matches['week']*7;
112 $duration.=$days . 'D';
114 if ($matches['minute'] || $matches['second'] || $matches['hour']) {
117 if ($matches['hour'])
118 $duration.=$matches['hour'].'H';
120 if ($matches['minute'])
121 $duration.=$matches['minute'].'M';
123 if ($matches['second'])
124 $duration.=$matches['second'].'S';
128 if ($duration==='P') {
131 $iv = new \DateInterval($duration);
132 if ($invert) $iv->invert = true;
149 foreach($parts as $part) {
150 if (isset($matches[$part]) && $matches[$part]) {
151 $newDur.=' '.$matches[$part] . ' ' . $part . 's';
155 $newDur = ($matches['plusminus']==='-'?'-':'+') . trim($newDur);
156 if ($newDur === '+') { $newDur = '+0 seconds'; };
162 * Parses either a Date or DateTime, or Duration value.
164 * @param string $date
165 * @param DateTimeZone|string $referenceTZ
166 * @return DateTime|DateInterval
168 static public function parse($date, $referenceTZ = null) {
170 if ($date[0]==='P' || ($date[0]==='-' && $date[1]==='P')) {
171 return self::parseDuration($date);
172 } elseif (strlen($date)===8) {
173 return self::parseDate($date);
175 return self::parseDateTime($date, $referenceTZ);