Skip to content

Commit b224e74

Browse files
mcq8nikic
authored andcommitted
Fixed bug #72096 Swatch time value incorrect for dates before 1970
1 parent bab0b99 commit b224e74

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? 2017 PHP 7.0.18
44

5+
- Date:
6+
. Fixed bug #72096 (Swatch time value incorrect for dates before 1970). (mcq8)
7+
58
- DOM:
69
. Fixed bug #74004 (LIBXML_NOWARNING flag ingnored on loadHTML*).
710
(somedaysummer)

ext/date/php_date.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,11 +1141,12 @@ static zend_string *date_format(char *format, size_t format_len, timelib_time *t
11411141
case 'a': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "pm" : "am"); break;
11421142
case 'A': length = slprintf(buffer, 32, "%s", t->h >= 12 ? "PM" : "AM"); break;
11431143
case 'B': {
1144-
int retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
1145-
while (retval < 0) {
1146-
retval += 1000;
1144+
int retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
1145+
if (retval < 0) {
1146+
retval += 864000;
11471147
}
1148-
retval = retval % 1000;
1148+
/* Make sure to do this on a positive int to avoid rounding errors */
1149+
retval = (retval / 864) % 1000;
11491150
length = slprintf(buffer, 32, "%03d", retval);
11501151
break;
11511152
}
@@ -1336,11 +1337,12 @@ PHPAPI int php_idate(char format, time_t ts, int localtime)
13361337

13371338
/* Swatch Beat a.k.a. Internet Time */
13381339
case 'B':
1339-
retval = (((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10) / 864);
1340-
while (retval < 0) {
1341-
retval += 1000;
1340+
retval = ((((long)t->sse)-(((long)t->sse) - ((((long)t->sse) % 86400) + 3600))) * 10);
1341+
if (retval < 0) {
1342+
retval += 864000;
13421343
}
1343-
retval = retval % 1000;
1344+
/* Make sure to do this on a positive int to avoid rounding errors */
1345+
retval = (retval / 864) % 1000;
13441346
break;
13451347

13461348
/* time */

ext/date/tests/bug72096.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
--TEST--
2+
Bug #72096: Swatch time value incorrect for dates before 1970
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
for ($unix = 1461283200; $unix <= 1461369600; $unix += 8000) {
8+
echo "Time:", gmdate('Y-m-d H:i:s = B', $unix), PHP_EOL;
9+
echo "Time:", gmdate('Y-m-d H:i:s = B', $unix - 82 * 365 * 24 * 3600), PHP_EOL;
10+
}
11+
?>
12+
--EXPECT--
13+
Time:2016-04-22 00:00:00 = 041
14+
Time:1934-05-13 00:00:00 = 041
15+
Time:2016-04-22 02:13:20 = 134
16+
Time:1934-05-13 02:13:20 = 134
17+
Time:2016-04-22 04:26:40 = 226
18+
Time:1934-05-13 04:26:40 = 226
19+
Time:2016-04-22 06:40:00 = 319
20+
Time:1934-05-13 06:40:00 = 319
21+
Time:2016-04-22 08:53:20 = 412
22+
Time:1934-05-13 08:53:20 = 412
23+
Time:2016-04-22 11:06:40 = 504
24+
Time:1934-05-13 11:06:40 = 504
25+
Time:2016-04-22 13:20:00 = 597
26+
Time:1934-05-13 13:20:00 = 597
27+
Time:2016-04-22 15:33:20 = 689
28+
Time:1934-05-13 15:33:20 = 689
29+
Time:2016-04-22 17:46:40 = 782
30+
Time:1934-05-13 17:46:40 = 782
31+
Time:2016-04-22 20:00:00 = 875
32+
Time:1934-05-13 20:00:00 = 875
33+
Time:2016-04-22 22:13:20 = 967
34+
Time:1934-05-13 22:13:20 = 967

0 commit comments

Comments
 (0)