summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerick Rethans <github@derickrethans.nl>2018-11-28 20:35:16 +0000
committerChristoph M. Becker <cmbecker69@gmx.de>2018-12-03 14:39:39 +0100
commitea302f8b085466da1f417070f966ccce1e5ea9d2 (patch)
treefec2fa3e0a0880071e8de9b1c26853cf12d9ffde
parent3a144d3f7f6bad308e2bf112ebf16829eb298f20 (diff)
downloadphp-git-ea302f8b085466da1f417070f966ccce1e5ea9d2.tar.gz
Fixed bug #77097 (DateTime::diff gives wrong diff when the actual diff is less than 1 second)
(cherry picked from commit dd48f0899dccf61c62313fbe8d1fb4d49d2b44f2)
-rw-r--r--ext/date/lib/interval.c5
-rw-r--r--ext/date/lib/timelib.h4
-rw-r--r--ext/date/tests/bug77097.phpt33
3 files changed, 39 insertions, 3 deletions
diff --git a/ext/date/lib/interval.c b/ext/date/lib/interval.c
index e9fa274563..d1f7db107a 100644
--- a/ext/date/lib/interval.c
+++ b/ext/date/lib/interval.c
@@ -35,7 +35,10 @@ timelib_rel_time *timelib_diff(timelib_time *one, timelib_time *two)
rt = timelib_rel_time_ctor();
rt->invert = 0;
- if (one->sse > two->sse) {
+ if (
+ (one->sse > two->sse) ||
+ (one->sse == two->sse && one->us > two->us)
+ ) {
swp = two;
two = one;
one = swp;
diff --git a/ext/date/lib/timelib.h b/ext/date/lib/timelib.h
index e272cfcd13..537c75a695 100644
--- a/ext/date/lib/timelib.h
+++ b/ext/date/lib/timelib.h
@@ -322,8 +322,8 @@ typedef struct _timelib_tzdb {
#endif
#define TIMELIB_VERSION 201801
-#define TIMELIB_EXTENDED_VERSION 20180103
-#define TIMELIB_ASCII_VERSION "2018.01RC1"
+#define TIMELIB_EXTENDED_VERSION 20180104
+#define TIMELIB_ASCII_VERSION "2018.01RC2"
#define TIMELIB_NONE 0x00
#define TIMELIB_OVERRIDE_TIME 0x01
diff --git a/ext/date/tests/bug77097.phpt b/ext/date/tests/bug77097.phpt
new file mode 100644
index 0000000000..effaad4900
--- /dev/null
+++ b/ext/date/tests/bug77097.phpt
@@ -0,0 +1,33 @@
+--TEST--
+Bug #77097 (DateTime::diff gives wrong diff when the actual diff is less than 1 second)
+--FILE--
+<?php
+$now = new DateTime('2018-11-03 11:34:20.781751');
+$ago = new DateTime('2018-11-03 11:34:20.000000');
+
+$diff = $now->diff($ago);
+var_dump($diff->invert, $diff->s, $diff->f);
+
+$diff = $ago->diff($now);
+var_dump($diff->invert, $diff->s, $diff->f);
+
+$diff = $now->diff($ago, true);
+var_dump($diff->invert, $diff->s, $diff->f);
+
+$diff = $ago->diff($now, true);
+var_dump($diff->invert, $diff->s, $diff->f);
+?>
+--EXPECTF--
+int(1)
+int(0)
+float(0.781751)
+int(0)
+int(0)
+float(0.781751)
+int(0)
+int(0)
+float(0.781751)
+int(0)
+int(0)
+float(0.781751)
+