]> granicus.if.org Git - php/commitdiff
Fixed bug #72096 Swatch time value incorrect for dates before 1970
authormcq8 <php@mcq8.be>
Fri, 3 Jun 2016 19:28:20 +0000 (19:28 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 9 Mar 2017 15:44:02 +0000 (16:44 +0100)
NEWS
ext/date/php_date.c
ext/date/tests/bug72096.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index bb94fce46b2831b27ed0cf97d6c25c29800ae1ca..1498b609780f3f405c808984ce25712db790a964 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ PHP                                                                        NEWS
 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
 ?? ??? 2017 PHP 7.0.18
 
+- Date:
+  . Fixed bug #72096 (Swatch time value incorrect for dates before 1970). (mcq8)
+
 - DOM:
   . Fixed bug #74004 (LIBXML_NOWARNING flag ingnored on loadHTML*).
     (somedaysummer)
index 8f81117d111e3427a48931e80431c4ee851e5e90..630354cc7a6e50737457f80a3cfb02ba0515d1fd 100644 (file)
@@ -1141,11 +1141,12 @@ static zend_string *date_format(char *format, size_t format_len, timelib_time *t
                        case 'a': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "pm" : "am"); break;
                        case 'A': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "PM" : "AM"); break;
                        case 'B': {
-                               int retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
-                               while (retval < 0) {
-                                       retval += 1000;
+                               int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
+                               if (retval < 0) {
+                                       retval += 864000;
                                }
-                               retval = retval % 1000;
+                               /* Make sure to do this on a positive int to avoid rounding errors */
+                               retval = (retval / 864)  % 1000;
                                length = slprintf(buffer, 32, "%03d", retval);
                                break;
                        }
@@ -1336,11 +1337,12 @@ PHPAPI int php_idate(char format, time_t ts, int localtime)
 
                /* Swatch Beat a.k.a. Internet Time */
                case 'B':
-                       retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
-                       while (retval < 0) {
-                               retval += 1000;
+                       retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
+                       if (retval < 0) {
+                               retval += 864000;
                        }
-                       retval = retval % 1000;
+                       /* Make sure to do this on a positive int to avoid rounding errors */
+                       retval = (retval / 864) % 1000;
                        break;
 
                /* time */
diff --git a/ext/date/tests/bug72096.phpt b/ext/date/tests/bug72096.phpt
new file mode 100644 (file)
index 0000000..1a4a219
--- /dev/null
@@ -0,0 +1,34 @@
+--TEST--
+Bug #72096: Swatch time value incorrect for dates before 1970
+--INI--
+date.timezone=UTC
+--FILE--
+<?php
+for ($unix = 1461283200; $unix <= 1461369600; $unix += 8000) {
+       echo "Time:", gmdate('Y-m-d H:i:s = B', $unix), PHP_EOL;
+       echo "Time:", gmdate('Y-m-d H:i:s = B', $unix - 82 * 365 * 24 * 3600), PHP_EOL;
+}
+?>
+--EXPECT--
+Time:2016-04-22 00:00:00 = 041
+Time:1934-05-13 00:00:00 = 041
+Time:2016-04-22 02:13:20 = 134
+Time:1934-05-13 02:13:20 = 134
+Time:2016-04-22 04:26:40 = 226
+Time:1934-05-13 04:26:40 = 226
+Time:2016-04-22 06:40:00 = 319
+Time:1934-05-13 06:40:00 = 319
+Time:2016-04-22 08:53:20 = 412
+Time:1934-05-13 08:53:20 = 412
+Time:2016-04-22 11:06:40 = 504
+Time:1934-05-13 11:06:40 = 504
+Time:2016-04-22 13:20:00 = 597
+Time:1934-05-13 13:20:00 = 597
+Time:2016-04-22 15:33:20 = 689
+Time:1934-05-13 15:33:20 = 689
+Time:2016-04-22 17:46:40 = 782
+Time:1934-05-13 17:46:40 = 782
+Time:2016-04-22 20:00:00 = 875
+Time:1934-05-13 20:00:00 = 875
+Time:2016-04-22 22:13:20 = 967
+Time:1934-05-13 22:13:20 = 967