3 namespace Sabre\VObject\Property;
10 * This element is used for iCalendar properties such as the DTSTART property.
11 * It basically provides a few helper functions that make it easier to deal
12 * with these. It supports both DATE-TIME and DATE values.
14 * In order to use this correctly, you must call setDateTime and getDateTime to
15 * retrieve and modify dates respectively.
17 * If you use the 'value' or properties directly, this object does not keep
18 * reference and results might appear incorrectly.
20 * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
21 * @author Evert Pot (http://www.rooftopsolutions.nl/)
22 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
24 class DateTime extends VObject\Property {
27 * Local 'floating' time
37 * Local time plus timezone
42 * Only a date, time is ignored
47 * DateTime representation
61 * Updates the Date and Time.
63 * @param \DateTime $dt
64 * @param int $dateType
67 public function setDateTime(\DateTime $dt, $dateType = self::LOCALTZ) {
72 $this->setValue($dt->format('Ymd\\THis'));
73 $this->offsetUnset('VALUE');
74 $this->offsetUnset('TZID');
75 $this->offsetSet('VALUE','DATE-TIME');
78 $dt->setTimeZone(new \DateTimeZone('UTC'));
79 $this->setValue($dt->format('Ymd\\THis\\Z'));
80 $this->offsetUnset('VALUE');
81 $this->offsetUnset('TZID');
82 $this->offsetSet('VALUE','DATE-TIME');
85 $this->setValue($dt->format('Ymd\\THis'));
86 $this->offsetUnset('VALUE');
87 $this->offsetUnset('TZID');
88 $this->offsetSet('VALUE','DATE-TIME');
89 $this->offsetSet('TZID', $dt->getTimeZone()->getName());
92 $this->setValue($dt->format('Ymd'));
93 $this->offsetUnset('VALUE');
94 $this->offsetUnset('TZID');
95 $this->offsetSet('VALUE','DATE');
98 throw new \InvalidArgumentException('You must pass a valid dateType constant');
101 $this->dateTime = $dt;
102 $this->dateType = $dateType;
107 * Returns the current DateTime value.
109 * If no value was set, this method returns null.
111 * @return \DateTime|null
113 public function getDateTime() {
116 return $this->dateTime;
121 ) = self::parseData($this->value, $this);
122 return $this->dateTime;
127 * Returns the type of Date format.
129 * This method returns one of the format constants. If no date was set,
130 * this method will return null.
134 public function getDateType() {
137 return $this->dateType;
142 ) = self::parseData($this->value, $this);
143 return $this->dateType;
148 * Parses the internal data structure to figure out what the current date
151 * The returned array contains two elements:
152 * 1. A 'DateType' constant (as defined on this class), or null.
153 * 2. A DateTime object (or null)
155 * @param string|null $propertyValue The string to parse (yymmdd or
156 * ymmddThhmmss, etc..)
157 * @param \Sabre\VObject\Property|null $property The instance of the
158 * property we're parsing.
161 static public function parseData($propertyValue, VObject\Property $property = null) {
163 if (is_null($propertyValue)) {
164 return array(null, null);
167 $date = '(?P<year>[1-2][0-9]{3})(?P<month>[0-1][0-9])(?P<date>[0-3][0-9])';
168 $time = '(?P<hour>[0-2][0-9])(?P<minute>[0-5][0-9])(?P<second>[0-5][0-9])';
169 $regex = "/^$date(T$time(?P<isutc>Z)?)?$/";
171 if (!preg_match($regex, $propertyValue, $matches)) {
172 throw new \InvalidArgumentException($propertyValue . ' is not a valid \DateTime or Date string');
175 if (!isset($matches['hour'])) {
179 new \DateTime($matches['year'] . '-' . $matches['month'] . '-' . $matches['date'] . ' 00:00:00', new \DateTimeZone('UTC')),
184 $matches['year'] .'-' .
185 $matches['month'] . '-' .
186 $matches['date'] . ' ' .
187 $matches['hour'] . ':' .
188 $matches['minute'] . ':' .
191 if (isset($matches['isutc'])) {
192 $dt = new \DateTime($dateStr,new \DateTimeZone('UTC'));
193 $dt->setTimeZone(new \DateTimeZone('UTC'));
200 // Finding the timezone.
201 $tzid = $property['TZID'];
203 // This was a floating time string. This implies we use the
204 // timezone from date_default_timezone_set / date.timezone ini
208 new \DateTime($dateStr)
212 // To look up the timezone, we must first find the VCALENDAR component.
214 while($root->parent) {
215 $root = $root->parent;
217 if ($root->name === 'VCALENDAR') {
218 $tz = VObject\TimeZoneUtil::getTimeZone((string)$tzid, $root);
220 $tz = VObject\TimeZoneUtil::getTimeZone((string)$tzid);
223 $dt = new \DateTime($dateStr, $tz);
224 $dt->setTimeZone($tz);