diff -r f8f63e944375 sys/src/cmd/webcookies.c --- a/sys/src/cmd/webcookies.c Fri Jul 17 16:53:20 2020 +0200 +++ b/sys/src/cmd/webcookies.c Sun Jul 19 11:23:07 2020 -0700 @@ -606,143 +606,25 @@ } /* + * Parse a date in one of these formats: * Sunday, 25-Jan-2002 12:24:36 GMT * Sunday, 25 Jan 2002 12:24:36 GMT * Sun, 25 Jan 02 12:24:36 GMT */ -int -isleap(int year) -{ - return year%4==0 && (year%100!=0 || year%400==0); -} - uint strtotime(char *s) { - char *os; - int i; + char **f, *fmts[] = { + "?WW, ?DD-?MM-?YYYY hh:mm:ss ?Z", + "?WW, ?DD ?MM ?YYYY hh:mm:ss ?Z", + nil, + }; Tm tm; - static int mday[2][12] = { - 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, - 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, - }; - static char *wday[] = { - "Sunday", "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday", - }; - static char *mon[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", - }; - - memset(&tm, 0, sizeof(tm)); - - os = s; - /* Sunday, */ - for(i=0; i mday[isleap(tm.year)][tm.mon]){ - if(debug) - fprint(2, "invalid day of month (%s)\n", os); - return -1; - } - tm.year -= 1900; - if(*s++ != ' '){ - if(debug) - fprint(2, "bad year separator (%s)\n", os); - return -1; - } - - if(!isdigit(s[0]) || !isdigit(s[1]) || s[2]!=':' - || !isdigit(s[3]) || !isdigit(s[4]) || s[5]!=':' - || !isdigit(s[6]) || !isdigit(s[7]) || s[8]!=' '){ - if(debug) - fprint(2, "bad time (%s)\n", os); - return -1; - } - - tm.hour = strtol(s, 0, 10); - tm.min = strtol(s+3, 0, 10); - tm.sec = strtol(s+6, 0, 10); - if(tm.hour >= 24 || tm.min >= 60 || tm.sec >= 60){ - if(debug) - fprint(2, "invalid time (%s)\n", os); - return -1; - } - s += 9; - - if(cistrcmp(s, "GMT") != 0){ - if(debug) - fprint(2, "time zone not GMT (%s)\n", os); - return -1; - } - strcpy(tm.zone, "GMT"); - tm.yday = 0; - return tm2sec(&tm); + for(f = fmts; *f != nil; f++) + if(tmparse(&tm, *f, s, nil, nil) != nil) + return tmnorm(&tm); + return -1; } /*