]> granicus.if.org Git - php/commitdiff
- Fixed bug where the DateTime object got changed while using date_diff().
authorDerick Rethans <derick@php.net>
Fri, 17 Jun 2011 16:38:23 +0000 (16:38 +0000)
committerDerick Rethans <derick@php.net>
Fri, 17 Jun 2011 16:38:23 +0000 (16:38 +0000)
NEWS
ext/date/lib/interval.c
ext/date/tests/date_diff1.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 563669e6707b76d78cf77e8ceda96479f2bd3243..6a7e18c358efb0ee30ec8d81448bf2728eefbf89 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,11 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 2011, PHP 5.3.7
 
+- DateTime extension:
+  . Fixed bug where the DateTime object got changed while using date_diff().
+    (Derick)
+
+
 16 Jun 2011, PHP 5.3.7 RC1
 - Upgraded bundled SQLite to version 3.7.6.3. (Scott)
 - Upgraded bundled PCRE to version 8.12. (Scott)
index 6ac07419b4bce2f1ec860d23ca9f5929353ef09a..c8200a48c349ff07524966430f342305f0c61973 100644 (file)
@@ -25,6 +25,7 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
        timelib_rel_time *rt;
        timelib_time *swp;
        timelib_sll dst_h_corr = 0, dst_m_corr = 0;
+       timelib_time one_backup, two_backup;
 
        rt = timelib_rel_time_ctor();
        rt->invert = 0;
@@ -45,6 +46,10 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
                dst_m_corr = ((two->z - one->z) % 3600) / 60;
        }
 
+       /* Save old TZ info */
+       memcpy(&one_backup, one, sizeof(one_backup));
+       memcpy(&two_backup, two, sizeof(two_backup));
+
     timelib_apply_localtime(one, 0);
     timelib_apply_localtime(two, 0);
 
@@ -58,8 +63,9 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
 
        timelib_do_rel_normalize(rt->invert ? one : two, rt);
 
-    timelib_apply_localtime(one, 1);
-    timelib_apply_localtime(two, 1);
+       /* Restore old TZ info */
+       memcpy(one, &one_backup, sizeof(one_backup));
+       memcpy(two, &two_backup, sizeof(two_backup));
 
        return rt;
 }
diff --git a/ext/date/tests/date_diff1.phpt b/ext/date/tests/date_diff1.phpt
new file mode 100644 (file)
index 0000000..cf32fcb
--- /dev/null
@@ -0,0 +1,48 @@
+--TEST--
+Test for date_diff with timezone abbreviations.
+--INI--
+date.timezone=Europe/London
+--FILE--
+<?php
+$start = new DateTime('2010-10-04 02:18:48 EDT');
+$end   = new DateTime('2010-11-06 18:38:28 EDT');
+$int = $start->diff($end);
+var_dump($start);
+var_dump($end);
+var_dump($int);
+?>
+--EXPECT--
+object(DateTime)#1 (3) {
+  ["date"]=>
+  string(19) "2010-10-04 02:18:48"
+  ["timezone_type"]=>
+  int(2)
+  ["timezone"]=>
+  string(3) "EDT"
+}
+object(DateTime)#2 (3) {
+  ["date"]=>
+  string(19) "2010-11-06 18:38:28"
+  ["timezone_type"]=>
+  int(2)
+  ["timezone"]=>
+  string(3) "EDT"
+}
+object(DateInterval)#3 (8) {
+  ["y"]=>
+  int(0)
+  ["m"]=>
+  int(1)
+  ["d"]=>
+  int(2)
+  ["h"]=>
+  int(16)
+  ["i"]=>
+  int(19)
+  ["s"]=>
+  int(40)
+  ["invert"]=>
+  int(0)
+  ["days"]=>
+  int(33)
+}