6 * This element is used for iCalendar properties such as the DTSTART property.
7 * It basically provides a few helper functions that make it easier to deal
8 * with these. It supports both DATE-TIME and DATE values.
10 * In order to use this correctly, you must call setDateTime and getDateTime to
11 * retrieve and modify dates respectively.
13 * If you use the 'value' or properties directly, this object does not keep
14 * reference and results might appear incorrectly.
18 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
19 * @author Evert Pot (http://www.rooftopsolutions.nl/)
20 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
22 class Sabre_VObject_Property_DateTime extends Sabre_VObject_Property {
25 * Local 'floating' time
35 * Local time plus timezone
40 * Only a date, time is ignored
45 * DateTime representation
59 * Updates the Date and Time.
62 * @param int $dateType
65 public function setDateTime(DateTime $dt, $dateType = self::LOCALTZ) {
70 $this->setValue($dt->format('Ymd\\THis'));
71 $this->offsetUnset('VALUE');
72 $this->offsetUnset('TZID');
73 $this->offsetSet('VALUE','DATE-TIME');
76 $dt->setTimeZone(new DateTimeZone('UTC'));
77 $this->setValue($dt->format('Ymd\\THis\\Z'));
78 $this->offsetUnset('VALUE');
79 $this->offsetUnset('TZID');
80 $this->offsetSet('VALUE','DATE-TIME');
83 $this->setValue($dt->format('Ymd\\THis'));
84 $this->offsetUnset('VALUE');
85 $this->offsetUnset('TZID');
86 $this->offsetSet('VALUE','DATE-TIME');
87 $this->offsetSet('TZID', $dt->getTimeZone()->getName());
90 $this->setValue($dt->format('Ymd'));
91 $this->offsetUnset('VALUE');
92 $this->offsetUnset('TZID');
93 $this->offsetSet('VALUE','DATE');
96 throw new InvalidArgumentException('You must pass a valid dateType constant');
99 $this->dateTime = $dt;
100 $this->dateType = $dateType;
105 * Returns the current DateTime value.
107 * If no value was set, this method returns null.
109 * @return DateTime|null
111 public function getDateTime() {
114 return $this->dateTime;
119 ) = self::parseData($this->value, $this);
120 return $this->dateTime;
125 * Returns the type of Date format.
127 * This method returns one of the format constants. If no date was set,
128 * this method will return null.
132 public function getDateType() {
135 return $this->dateType;
140 ) = self::parseData($this->value, $this);
141 return $this->dateType;
146 * Parses the internal data structure to figure out what the current date
149 * The returned array contains two elements:
150 * 1. A 'DateType' constant (as defined on this class), or null.
151 * 2. A DateTime object (or null)
153 * @param string|null $propertyValue The string to parse (yymmdd or
154 * ymmddThhmmss, etc..)
155 * @param Sabre_VObject_Property|null $property The instance of the
156 * property we're parsing.
159 static public function parseData($propertyValue, Sabre_VObject_Property $property = null) {
161 if (is_null($propertyValue)) {
162 return array(null, null);
165 $date = '(?P<year>[1-2][0-9]{3})(?P<month>[0-1][0-9])(?P<date>[0-3][0-9])';
166 $time = '(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-5][0-9])';
167 $regex = "/^$date(T$time(?P<isutc>Z)?)?$/";
169 if (!preg_match($regex, $propertyValue, $matches)) {
170 throw new InvalidArgumentException($propertyValue . ' is not a valid DateTime or Date string');
173 if (!isset($matches['hour'])) {
177 new DateTime($matches['year'] . '-' . $matches['month'] . '-' . $matches['date'] . ' 00:00:00'),
182 $matches['year'] .'-' .
183 $matches['month'] . '-' .
184 $matches['date'] . ' ' .
185 $matches['hour'] . ':' .
186 $matches['minute'] . ':' .
189 if (isset($matches['isutc'])) {
190 $dt = new DateTime($dateStr,new DateTimeZone('UTC'));
191 $dt->setTimeZone(new DateTimeZone('UTC'));
198 // Finding the timezone.
199 $tzid = $property['TZID'];
203 new DateTime($dateStr)
207 // To look up the timezone, we must first find the VCALENDAR component.
209 while($root->parent) {
210 $root = $root->parent;
212 if ($root->name === 'VCALENDAR') {
213 $tz = Sabre_VObject_TimeZoneUtil::getTimeZone((string)$tzid, $root);
215 $tz = Sabre_VObject_TimeZoneUtil::getTimeZone((string)$tzid);
218 $dt = new DateTime($dateStr, $tz);
219 $dt->setTimeZone($tz);