php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login

Patch bug-datetime-diff-calculations for Date/time related Bug #70126

Patch version 2015-07-24 09:51 UTC

Return to Bug #70126 | Download this patch
Patch Revisions:

Developer: php@tul.io

diff --git a/ext/date/lib/interval.c b/ext/date/lib/interval.c
index 67e81d5..5b9370d 100644
--- a/ext/date/lib/interval.c
+++ b/ext/date/lib/interval.c
@@ -68,7 +68,7 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
 
 	rt->days = abs(floor((one->sse - two->sse - (dst_h_corr * 3600) - (dst_m_corr * 60)) / 86400));
 
-	timelib_do_rel_normalize(rt->invert ? one : two, rt);
+	timelib_do_rel_normalize(one, rt);
 
 	/* We need to do this after normalisation otherwise we can't get "24H" */
 	if (one_backup.dst == 1 && two_backup.dst == 0 && two->sse >= one->sse + 86400) {
diff --git a/ext/date/lib/tm2unixtime.c b/ext/date/lib/tm2unixtime.c
index 2714c9a..e39f837 100644
--- a/ext/date/lib/tm2unixtime.c
+++ b/ext/date/lib/tm2unixtime.c
@@ -58,7 +58,7 @@ static void dec_month(timelib_sll *y, timelib_sll *m)
 	}
 }
 
-static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d, timelib_sll invert)
+static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_m, timelib_sll *y, timelib_sll *m, timelib_sll *d)
 {
 	timelib_sll leapyear;
 	timelib_sll month, year;
@@ -72,28 +72,15 @@ static void do_range_limit_days_relative(timelib_sll *base_y, timelib_sll *base_
 /*
 	printf( "S: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
 */
-	if (!invert) {
-		while (*d < 0) {
-			dec_month(&year, &month);
-			leapyear = timelib_is_leap(year);
-			days = leapyear ? days_in_month_leap[month] : days_in_month[month];
+	while (*d < 0) {
+		leapyear = timelib_is_leap(year);
+		days = leapyear ? days_in_month_leap[month] : days_in_month[month];
 
-			/* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
+		/* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
 
-			*d += days;
-			(*m)--;
-		}
-	} else {
-		while (*d < 0) {
-			leapyear = timelib_is_leap(year);
-			days = leapyear ? days_in_month_leap[month] : days_in_month[month];
-
-			/* printf( "I  Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days); */
-
-			*d += days;
-			(*m)--;
-			inc_month(&year, &month);
-		}
+		*d += days;
+		(*m)--;
+		inc_month(&year, &month);
 	}
 	/*
 	printf( "E: Y%d M%d   %d %d %d   %d\n", year, month, *y, *m, *d, days);
@@ -174,7 +161,7 @@ void timelib_do_rel_normalize(timelib_time *base, timelib_rel_time *rt)
 	do_range_limit(0, 24, 24, &rt->h, &rt->d);
 	do_range_limit(0, 12, 12, &rt->m, &rt->y);
 
-	do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d, rt->invert);
+	do_range_limit_days_relative(&base->y, &base->m, &rt->y, &rt->m, &rt->d);
 	do_range_limit(0, 12, 12, &rt->m, &rt->y);
 }
 
diff --git a/ext/date/tests/DateTime_diff-february.phpt b/ext/date/tests/DateTime_diff-february.phpt
index 7705b12..5dbf37b 100644
--- a/ext/date/tests/DateTime_diff-february.phpt
+++ b/ext/date/tests/DateTime_diff-february.phpt
@@ -30,7 +30,7 @@ test_bug_49081__16: DIFF: 2010-02-28 00:00:00 EST - 2010-01-29 00:00:00 EST = **
 test_bug_49081__17: DIFF: 2010-02-28 00:00:00 EST - 2010-01-28 00:00:00 EST = **P+0Y1M0DT0H0M0S**
 test_bug_49081__18: DIFF: 2010-02-28 00:00:00 EST - 2010-01-27 00:00:00 EST = **P+0Y1M1DT0H0M0S**
 test_bug_49081__19: DIFF: 2010-03-01 00:00:00 EST - 2010-01-01 00:00:00 EST = **P+0Y2M0DT0H0M0S**
-test_bug_49081__20: DIFF: 2010-03-01 00:00:00 EST - 2010-01-31 00:00:00 EST = **P+0Y0M29DT0H0M0S**
+test_bug_49081__20: DIFF: 2010-03-01 00:00:00 EST - 2010-01-31 00:00:00 EST = **P+0Y1M1DT0H0M0S**
 test_bug_49081__21: DIFF: 2010-03-27 00:00:00 EDT - 2010-01-31 00:00:00 EST = **P+0Y1M24DT0H0M0S**
 test_bug_49081__22: DIFF: 2010-03-28 00:00:00 EDT - 2010-01-31 00:00:00 EST = **P+0Y1M25DT0H0M0S**
 test_bug_49081__23: DIFF: 2010-03-29 00:00:00 EDT - 2010-01-31 00:00:00 EST = **P+0Y1M26DT0H0M0S**
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 17:01:58 2024 UTC