]> granicus.if.org Git - php/commitdiff
Fixed bug #63740 (strtotime seems to use both sunday and monday as start of week)
authorDerick Rethans <github@derickrethans.nl>
Wed, 18 May 2016 11:19:11 +0000 (12:19 +0100)
committerDerick Rethans <github@derickrethans.nl>
Wed, 18 May 2016 11:19:11 +0000 (12:19 +0100)
NEWS
ext/date/lib/timelib.c
ext/date/lib/timelib.h
ext/date/lib/tm2unixtime.c
ext/date/tests/bug63740.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index 3cf7aa2927e1aa0184ec3291d5d9ae1836be271a..4be12a5547ac877c07aa6cce7e76143fe3d7545e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -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:
index b938d9f99834603a49c6b02700a17c6a8ebec01f..b9fb66f00fbbfd8d24969cfebec582050c126350 100644 (file)
@@ -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;
+               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;
 }
index fe1069bddea18c6cdf6730ad72f37be51e85b7e4..9a59d89770b7927e211979b65106195598837440 100644 (file)
@@ -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
index 414cf1308200dd066c7e7d8212d6d6591de38ac0..57e0cef1be61956874402c02799ea25a8da0d60c 100644 (file)
@@ -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;
diff --git a/ext/date/tests/bug63740.phpt b/ext/date/tests/bug63740.phpt
new file mode 100644 (file)
index 0000000..18c5a57
--- /dev/null
@@ -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