- Fixed bug where the DateTime object got changed while using date_diff().

This commit is contained in:
Derick Rethans 2011-06-17 16:38:23 +00:00
parent 740285307d
commit 9437d4cea4
2 changed files with 56 additions and 2 deletions

View 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;
}

View file

@ -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)
}