[Pkg-owncloud-commits] [php-sabre-vobject] 11/32: Allowing 'reference timezones' for freebusy reports and other places.

David Prévot taffit at moszumanska.debian.org
Fri Nov 28 22:27:09 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabre-vobject.

commit 08e8b7f4aac4c4bb139a845a664197ed36b6d57b
Author: Evert Pot <me at evertpot.com>
Date:   Wed Nov 12 18:19:55 2014 -0500

    Allowing 'reference timezones' for freebusy reports and other places.
---
 lib/DateTimeParser.php              | 41 +++++++++++++++++++++++--------------
 lib/FreeBusyGenerator.php           | 39 ++++++++++++++++++++++++++++++++---
 lib/Property/ICalendar/DateTime.php | 36 ++++++++++++++++++++------------
 3 files changed, 85 insertions(+), 31 deletions(-)

diff --git a/lib/DateTimeParser.php b/lib/DateTimeParser.php
index e702b40..75aaa62 100644
--- a/lib/DateTimeParser.php
+++ b/lib/DateTimeParser.php
@@ -2,6 +2,12 @@
 
 namespace Sabre\VObject;
 
+use DateTime;
+use DateTimeZone;
+use DateInterval;
+use InvalidArgumentException;
+use LogicException;
+
 /**
  * DateTimeParser
  *
@@ -25,19 +31,19 @@ class DateTimeParser {
      * @param DateTimeZone $tz
      * @return DateTime
      */
-    static public function parseDateTime($dt, \DateTimeZone $tz = null) {
+    static public function parseDateTime($dt, DateTimeZone $tz = null) {
 
         // Format is YYYYMMDD + "T" + hhmmss
         $result = preg_match('/^([0-9]{4})([0-1][0-9])([0-3][0-9])T([0-2][0-9])([0-5][0-9])([0-5][0-9])([Z]?)$/',$dt,$matches);
 
         if (!$result) {
-            throw new \LogicException('The supplied iCalendar datetime value is incorrect: ' . $dt);
+            throw new LogicException('The supplied iCalendar datetime value is incorrect: ' . $dt);
         }
 
         if ($matches[7]==='Z' || is_null($tz)) {
-            $tz = new \DateTimeZone('UTC');
+            $tz = new DateTimeZone('UTC');
         }
-        $date = new \DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3] . ' ' . $matches[4] . ':' . $matches[5] .':' . $matches[6], $tz);
+        $date = new DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3] . ' ' . $matches[4] . ':' . $matches[5] .':' . $matches[6], $tz);
 
         // Still resetting the timezone, to normalize everything to UTC
         // $date->setTimeZone(new \DateTimeZone('UTC'));
@@ -46,21 +52,26 @@ class DateTimeParser {
     }
 
     /**
-     * Parses an iCalendar (rfc5545) formatted date and returns a DateTime object
+     * Parses an iCalendar (rfc5545) formatted date and returns a DateTime object.
      *
      * @param string $date
+     * @param DateTimeZone $tz
      * @return DateTime
      */
-    static public function parseDate($date) {
+    static public function parseDate($date, DateTimeZone $tz = null) {
 
         // Format is YYYYMMDD
         $result = preg_match('/^([0-9]{4})([0-1][0-9])([0-3][0-9])$/',$date,$matches);
 
         if (!$result) {
-            throw new \LogicException('The supplied iCalendar date value is incorrect: ' . $date);
+            throw new LogicException('The supplied iCalendar date value is incorrect: ' . $date);
+        }
+
+        if (is_null($tz)) {
+            $tz = new DateTimeZone('UTC');
         }
 
-        $date = new \DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3], new \DateTimeZone('UTC'));
+        $date = new DateTime($matches[1] . '-' . $matches[2] . '-' . $matches[3], $tz);
         return $date;
 
     }
@@ -79,7 +90,7 @@ class DateTimeParser {
 
         $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);
         if (!$result) {
-            throw new \LogicException('The supplied iCalendar duration value is incorrect: ' . $duration);
+            throw new LogicException('The supplied iCalendar duration value is incorrect: ' . $duration);
         }
 
         if (!$asString) {
@@ -128,7 +139,7 @@ class DateTimeParser {
             if ($duration==='P') {
                 $duration = 'PT0S';
             }
-            $iv = new \DateInterval($duration);
+            $iv = new DateInterval($duration);
             if ($invert) $iv->invert = true;
 
             return $iv;
@@ -164,15 +175,15 @@ class DateTimeParser {
      * Parses either a Date or DateTime, or Duration value.
      *
      * @param string $date
-     * @param DateTimeZone|string $referenceTZ
+     * @param DateTimeZone|string $referenceTz
      * @return DateTime|DateInterval
      */
-    static public function parse($date, $referenceTZ = null) {
+    static public function parse($date, $referenceTz = null) {
 
         if ($date[0]==='P' || ($date[0]==='-' && $date[1]==='P')) {
             return self::parseDuration($date);
         } elseif (strlen($date)===8) {
-            return self::parseDate($date);
+            return self::parseDate($date, $referenceTz);
         } else {
             return self::parseDateTime($date, $referenceTZ);
         }
@@ -284,7 +295,7 @@ class DateTimeParser {
                 $/x';
 
             if (!preg_match($regex, $date, $matches)) {
-                throw new \InvalidArgumentException('Invalid vCard date-time string: ' . $date);
+                throw new InvalidArgumentException('Invalid vCard date-time string: ' . $date);
             }
 
         }
@@ -387,7 +398,7 @@ class DateTimeParser {
                 $/x';
 
             if (!preg_match($regex, $date, $matches)) {
-                throw new \InvalidArgumentException('Invalid vCard time string: ' . $date);
+                throw new InvalidArgumentException('Invalid vCard time string: ' . $date);
             }
 
         }
diff --git a/lib/FreeBusyGenerator.php b/lib/FreeBusyGenerator.php
index f71d2db..a0f62c3 100644
--- a/lib/FreeBusyGenerator.php
+++ b/lib/FreeBusyGenerator.php
@@ -2,6 +2,7 @@
 
 namespace Sabre\VObject;
 
+use DateTimeZone;
 use Sabre\VObject\Component\VCalendar;
 use Sabre\VObject\Recur\EventIterator;
 
@@ -50,6 +51,21 @@ class FreeBusyGenerator {
     protected $baseObject;
 
     /**
+     * Reference timezone.
+     *
+     * When we are calculating busy times, and we come across so-called
+     * floating times (times without a timezone), we use the reference timezone
+     * instead.
+     *
+     * This is also used for all-day events.
+     *
+     * This defaults to UTC.
+     *
+     * @var DateTimeZone
+     */
+    protected $timeZone;
+
+    /**
      * Creates the generator.
      *
      * Check the setTimeRange and setObjects methods for details about the
@@ -58,9 +74,10 @@ class FreeBusyGenerator {
      * @param DateTime $start
      * @param DateTime $end
      * @param mixed $objects
+     * @param DateTimeZone $timeZone
      * @return void
      */
-    public function __construct(\DateTime $start = null, \DateTime $end = null, $objects = null) {
+    public function __construct(\DateTime $start = null, \DateTime $end = null, $objects = null, DateTimeZone $timeZone = null) {
 
         if ($start && $end) {
             $this->setTimeRange($start, $end);
@@ -69,6 +86,10 @@ class FreeBusyGenerator {
         if ($objects) {
             $this->setObjects($objects);
         }
+        if (is_null($timeZone)) {
+            $timeZone = new DateTimeZone('UTC');
+        }
+        $this->setTimeZone($timeZone);
 
     }
 
@@ -137,6 +158,18 @@ class FreeBusyGenerator {
     }
 
     /**
+     * Sets the reference timezone for floating times.
+     *
+     * @param DateTimeZone $timeZone
+     * @return void
+     */
+    public function setTimeZone(DateTimeZone $timeZone) {
+
+        $this->timeZone = $timeZone;
+
+    }
+
+    /**
      * Parses the input data and returns a correct VFREEBUSY object, wrapped in
      * a VCALENDAR.
      *
@@ -196,13 +229,13 @@ class FreeBusyGenerator {
 
                         } else {
 
-                            $startTime = $component->DTSTART->getDateTime();
+                            $startTime = $component->DTSTART->getDateTime($this->timeZone);
                             if ($this->end && $startTime > $this->end) {
                                 break;
                             }
                             $endTime = null;
                             if (isset($component->DTEND)) {
-                                $endTime = $component->DTEND->getDateTime();
+                                $endTime = $component->DTEND->getDateTime($this->timeZone);
                             } elseif (isset($component->DURATION)) {
                                 $duration = DateTimeParser::parseDuration((string)$component->DURATION);
                                 $endTime = clone $startTime;
diff --git a/lib/Property/ICalendar/DateTime.php b/lib/Property/ICalendar/DateTime.php
index 3d9ffbf..5e7eb59 100644
--- a/lib/Property/ICalendar/DateTime.php
+++ b/lib/Property/ICalendar/DateTime.php
@@ -2,11 +2,11 @@
 
 namespace Sabre\VObject\Property\ICalendar;
 
-use
-    Sabre\VObject\Property,
-    Sabre\VObject\Parser\MimeDir,
-    Sabre\VObject\DateTimeParser,
-    Sabre\VObject\TimeZoneUtil;
+use DateTimeZone;
+use Sabre\VObject\Property;
+use Sabre\VObject\Parser\MimeDir;
+use Sabre\VObject\DateTimeParser;
+use Sabre\VObject\TimeZoneUtil;
 
 /**
  * DateTime property
@@ -117,11 +117,16 @@ class DateTime extends Property {
      * first will be returned. To get an array with multiple values, call
      * getDateTimes.
      *
+     * If no timezone information is known, because it's either an all-day
+     * property or floating time, we will use the DateTimeZone argument to
+     * figure out the exact date.
+     *
+     * @param DateTimeZone $timeZone
      * @return \DateTime
      */
-    public function getDateTime() {
+    public function getDateTime(DateTimeZone $timeZone = null) {
 
-        $dt = $this->getDateTimes();
+        $dt = $this->getDateTimes($timeZone);
         if (!$dt) return null;
 
         return $dt[0];
@@ -131,20 +136,25 @@ class DateTime extends Property {
     /**
      * Returns multiple date-time values.
      *
+     * If no timezone information is known, because it's either an all-day
+     * property or floating time, we will use the DateTimeZone argument to
+     * figure out the exact date.
+     *
+     * @param DateTimeZone $timeZone
      * @return \DateTime[]
      */
-    public function getDateTimes() {
+    public function getDateTimes(DateTimeZone $timeZone = null) {
 
-        // Finding the timezone.
-        $tz = $this['TZID'];
+        // Does the property have a TZID?
+        $tzid = $this['TZID'];
 
-        if ($tz) {
-            $tz = TimeZoneUtil::getTimeZone((string)$tz, $this->root);
+        if ($tzid) {
+            $timeZone = TimeZoneUtil::getTimeZone((string)$tzid, $this->root);
         }
 
         $dts = array();
         foreach($this->getParts() as $part) {
-            $dts[] = DateTimeParser::parse($part, $tz);
+            $dts[] = DateTimeParser::parse($part, $timeZone);
         }
         return $dts;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git



More information about the Pkg-owncloud-commits mailing list