From 4b9857e7a4848f94a0961ea3ce72f737cbf4bfed Mon Sep 17 00:00:00 2001 From: Derick Rethans Date: Fri, 17 Jun 2011 16:38:23 +0000 Subject: [PATCH] - Fixed bug where the DateTime object got changed while using date_diff(). --- NEWS | 5 ++++ ext/date/lib/interval.c | 10 +++++-- ext/date/tests/date_diff1.phpt | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 ext/date/tests/date_diff1.phpt diff --git a/NEWS b/NEWS index 563669e670..6a7e18c358 100644 --- 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) diff --git a/ext/date/lib/interval.c b/ext/date/lib/interval.c index 6ac07419b4..c8200a48c3 100644 --- a/ext/date/lib/interval.c +++ b/ext/date/lib/interval.c @@ -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 index 0000000000..cf32fcbf3b --- /dev/null +++ b/ext/date/tests/date_diff1.phpt @@ -0,0 +1,48 @@ +--TEST-- +Test for date_diff with timezone abbreviations. +--INI-- +date.timezone=Europe/London +--FILE-- +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) +} -- 2.40.0