cvs: pear /Date/Date Span.php

[email protected] ("Firman Wandayandi") Sat, 10 May 2008 07:34:25 -0000
Newsgroups php.pear.cvs
Message-ID <cvsfirman1210404865@cvsserver>
firman		Sat May 10 07:34:25 2008 UTC

  Modified files:              
    /pear/Date/Date	Span.php 
  Log:
  Fixing bug #13376, still not fixed even it has closed already. The problem is PHP5 always passing by reference of the object, so when Date converted (in method) to UTC its also changes the source. The solution is create a fresh object of Date based on the source, done.
  
http://cvs.php.net/viewvc.cgi/pear/Date/Date/Span.php?r1=1.17&r2=1.18&diff_format=u
Index: pear/Date/Date/Span.php
diff -u pear/Date/Date/Span.php:1.17 pear/Date/Date/Span.php:1.18
--- pear/Date/Date/Span.php:1.17	Thu May  8 22:27:34 2008
+++ pear/Date/Date/Span.php	Sat May 10 07:34:25 2008
@@ -36,7 +36,7 @@
  * @copyright  1997-2006 Leandro Lucarella, Pierre-Alain Joye
  * @license    http://www.opensource.org/licenses/bsd-license.php
  *             BSD License
- * @version    CVS: $Id: Span.php,v 1.17 2008/05/08 22:27:34 c01234 Exp $
+ * @version    CVS: $Id: Span.php,v 1.18 2008/05/10 07:34:25 firman Exp $
  * @link       http://pear.php.net/package/Date
  * @since      File available since Release 1.4
  */
@@ -598,27 +598,38 @@
         if (!is_a($date1, 'date') or !is_a($date2, 'date')) {
             return false;
         }
-        $date1->toUTC();
-        $date2->toUTC();
-        if ($date1->after($date2)) {
-            list($date1, $date2) = array($date2, $date1);
+
+        // create a local copy of instance, in order avoid changes the object
+        // reference when its object has converted to UTC due PHP5 is always
+        // passed the object by reference.
+        $tdate1 = new Date($date1);
+        $tdate2 = new Date($date2);
+
+        // convert to UTC
+        $tdate1->toUTC();
+        $tdate2->toUTC();
+
+        if ($tdate1->after($tdate2)) {
+            list($tdate1, $tdate2) = array($tdate2, $tdate1);
         }
-        $days  = Date_Calc::dateDiff($date1->getDay(),
-                                     $date1->getMonth(),
-                                     $date1->getYear(),
-                                     $date2->getDay(),
-                                     $date2->getMonth(),
-                                     $date2->getYear());
-        $hours = $date2->getHour() - $date1->getHour();
-        $mins  = $date2->getMinute() - $date1->getMinute();
-        $secs  = $date2->getSecond() - $date1->getSecond();
+
+        $days  = Date_Calc::dateDiff($tdate1->getDay(),
+                                     $tdate1->getMonth(),
+                                     $tdate1->getYear(),
+                                     $tdate2->getDay(),
+                                     $tdate2->getMonth(),
+                                     $tdate2->getYear());
+
+        $hours = $tdate2->getHour() - $tdate1->getHour();
+        $mins  = $tdate2->getMinute() - $tdate1->getMinute();
+        $secs  = $tdate2->getSecond() - $tdate1->getSecond();
+
         $this->setFromSeconds($days * 86400 +
                               $hours * 3600 +
                               $mins * 60 + $secs);
         return true;
     }
 
-
     // }}}
     // {{{ copy()
 
@@ -650,11 +661,11 @@
     /**
      * Formats time span according to specified code (similar to
      * {@link Date::formatLikeStrftime()})
-     * 
+     *
      * Uses a code based on {@link http://www.php.net/strftime strftime()}.
-     * 
+     *
      * Formatting options:
-     * 
+     *
      *  - <b>%C</b> - Days with time, equivalent to '<b>%D, %H:%M:%S</b>'
      *  - <b>%d</b> - Total days as a float number
      *                  (2 days, 12 hours = 2.5 days)