com php-src: Fixed bug #72096 Swatch time value incorrect for dates before 1970: NEWS ext/date/php_date.c ext/date/tests/bug72 096.phpt

[email protected] (Nikita Popov)
Newsgroups php.cvs
Message-ID <[email protected]>
Commit:    b224e7426713befd7449117afd68355231f9077e
Author:    mcq8 <[email protected]>         Fri, 3 Jun 2016 19:28:20 +0000
Committer: Nikita Popov <[email protected]>      Thu, 9 Mar 2017 16:44:02 +0100
Parents:   bab0b99f376dac9170ac81382a5ed526938d595a
Branches:  PHP-7.0 PHP-7.1 master

Link:       http://git.php.net/?p=php-src.git;a=commitdiff;h=b224e7426713befd7449117afd68355231f9077e

Log:
Fixed bug #72096 Swatch time value incorrect for dates before 1970

Bugs:
https://bugs.php.net/72096

Changed paths:
  M  NEWS
  M  ext/date/php_date.c
  A  ext/date/tests/bug72096.phpt


Diff:
diff --git a/NEWS b/NEWS
index bb94fce..1498b60 100644
--- 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)
diff --git a/ext/date/php_date.c b/ext/date/php_date.c
index 8f81117..630354cc 100644
--- a/ext/date/php_date.c
+++ b/ext/date/php_date.c
@@ -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
index 0000000..1a4a219
--- /dev/null
+++ b/ext/date/tests/bug72096.phpt
@@ -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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.