Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed bug #63740 (strtotime seems to use both sunday and monday as st…
…art of week)
  • Loading branch information
derickr committed May 18, 2016
1 parent 0815f7f commit f43f6fc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 32 deletions.
2 changes: 2 additions & 0 deletions NEWS
Expand Up @@ -30,6 +30,8 @@ PHP NEWS
(Michael Sierks)

- Date:
. Fixed bug #63740 (strtotime seems to use both sunday and monday as start of
week). (Derick)
. Fixed bug #71889 (DateInterval::format Segmentation fault). (Thomas Punt)

- EXIF:
Expand Down
58 changes: 29 additions & 29 deletions ext/date/lib/timelib.c
Expand Up @@ -310,33 +310,33 @@ void timelib_dump_rel_time(timelib_rel_time *d)

timelib_long timelib_parse_tz_cor(char **ptr)
{
char *begin = *ptr, *end;
timelib_long tmp;

while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1:
case 2:
return HOUR(strtol(begin, NULL, 10));
break;
case 3:
case 4:
if (begin[1] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
return tmp;
} else if (begin[2] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return HOUR(tmp / 100) + tmp % 100;
}
case 5:
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
}
return 0;
char *begin = *ptr, *end;
timelib_long tmp;

while (isdigit(**ptr) || **ptr == ':') {
++*ptr;
}
end = *ptr;
switch (end - begin) {
case 1: /* H */
case 2: /* HH */
return HOUR(strtol(begin, NULL, 10));
break;

This comment has been minimized.

Copy link
@staabm

staabm May 18, 2016

Contributor

unnecessary break after return

case 3: /* H:M */
case 4: /* H:MM, HH:M, HHMM */
if (begin[1] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 2, NULL, 10);
return tmp;
} else if (begin[2] == ':') {
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
} else {
tmp = strtol(begin, NULL, 10);
return HOUR(tmp / 100) + tmp % 100;
}
case 5: /* HH:MM */
tmp = HOUR(strtol(begin, NULL, 10)) + strtol(begin + 3, NULL, 10);
return tmp;
}
return 0;
}
4 changes: 2 additions & 2 deletions ext/date/lib/timelib.h
Expand Up @@ -38,8 +38,8 @@
# define timelib_free free
#endif

#define TIMELIB_VERSION 201502
#define TIMELIB_ASCII_VERSION "2015.02"
#define TIMELIB_VERSION 201602
#define TIMELIB_ASCII_VERSION "2016.02"

#define TIMELIB_NONE 0x00
#define TIMELIB_OVERRIDE_TIME 0x01
Expand Down
10 changes: 9 additions & 1 deletion ext/date/lib/tm2unixtime.c
Expand Up @@ -152,9 +152,17 @@ static void do_adjust_for_weekday(timelib_time* time)
current_dow = timelib_day_of_week(time->y, time->m, time->d);
if (time->relative.weekday_behavior == 2)
{
if (time->relative.weekday == 0) {
/* To make "this week" work, where the current DOW is a "sunday" */
if (current_dow == 0 && time->relative.weekday != 0) {
time->relative.weekday = -6;
}

/* To make "sunday this week" work, where the current DOW is not a
* "sunday" */
if (time->relative.weekday == 0 && current_dow != 0) {
time->relative.weekday = 7;
}

time->d -= current_dow;
time->d += time->relative.weekday;
return;
Expand Down
41 changes: 41 additions & 0 deletions ext/date/tests/bug63740.phpt
@@ -0,0 +1,41 @@
--TEST--
Bug #63740 (strtotime seems to use both sunday and monday as start of week)
--FILE--
<?php
$dates = [
'2015-07-04',
'2015-07-05',
'2015-07-06',
'2015-07-07',
'2015-07-08',
'2015-07-09',
'2015-07-10',
'2015-07-11',
'2015-07-12',
'2015-07-13',
'2015-07-14',
];

foreach ( $dates as $date )
{
$dt = new DateTimeImmutable( "$date 00:00 UTC" );

echo $dt->format( "D Y-m-d H:i" ), "";

$dtn = $dt->modify( "this week" );

echo $dtn->format( "D Y-m-d H:i" ), "\n";
}
?>
--EXPECT--
Sat 2015-07-04 00:00 → Mon 2015-06-29 00:00
Sun 2015-07-05 00:00 → Mon 2015-06-29 00:00
Mon 2015-07-06 00:00 → Mon 2015-07-06 00:00
Tue 2015-07-07 00:00 → Mon 2015-07-06 00:00
Wed 2015-07-08 00:00 → Mon 2015-07-06 00:00
Thu 2015-07-09 00:00 → Mon 2015-07-06 00:00
Fri 2015-07-10 00:00 → Mon 2015-07-06 00:00
Sat 2015-07-11 00:00 → Mon 2015-07-06 00:00
Sun 2015-07-12 00:00 → Mon 2015-07-06 00:00
Mon 2015-07-13 00:00 → Mon 2015-07-13 00:00
Tue 2015-07-14 00:00 → Mon 2015-07-13 00:00

0 comments on commit f43f6fc

Please sign in to comment.