date handling: handle "AM"/"PM" on time

And be a bitmore careful about matching: if we don't recognize a word
or a number, we skip the whole thing, rather than trying the next character
in that word/number.

Finally: since ctime() adds the final '\n', don't add another one in test-date.
This commit is contained in:
Linus Torvalds
2005-05-01 12:34:56 -07:00
parent 198b0fb635
commit 68849b5442
2 changed files with 23 additions and 9 deletions

22
date.c
View File

@ -114,6 +114,15 @@ static int match_string(const char *date, const char *str)
return i; return i;
} }
static int skip_alpha(const char *date)
{
int i = 0;
do {
i++;
} while (isalpha(date[i]));
return i;
}
/* /*
* Parse month, weekday, or timezone name * Parse month, weekday, or timezone name
*/ */
@ -153,8 +162,14 @@ static int match_alpha(const char *date, struct tm *tm, int *offset)
} }
} }
if (match_string(date, "PM") == 2) {
if (tm->tm_hour > 0 && tm->tm_hour < 12)
tm->tm_hour += 12;
return 2;
}
/* BAD CRAP */ /* BAD CRAP */
return 0; return skip_alpha(date);
} }
static int is_date(int year, int month, int day, struct tm *tm) static int is_date(int year, int month, int day, struct tm *tm)
@ -332,14 +347,13 @@ static int match_tz(char *date, int *offp)
* a valid minute. We might want to check that the minutes * a valid minute. We might want to check that the minutes
* are divisible by 30 or something too. * are divisible by 30 or something too.
*/ */
if (min >= 60 || n < 3) if (min < 60 && n > 2) {
return 0;
offset = hour*60+min; offset = hour*60+min;
if (*date == '-') if (*date == '-')
offset = -offset; offset = -offset;
*offp = offset; *offp = offset;
}
return end - date; return end - date;
} }

View File

@ -14,7 +14,7 @@ int main(int argc, char **argv)
memcpy(result, "bad", 4); memcpy(result, "bad", 4);
parse_date(argv[i], result, sizeof(result)); parse_date(argv[i], result, sizeof(result));
t = strtoul(result, NULL, 0); t = strtoul(result, NULL, 0);
printf("%s -> %s -> %s\n", argv[i], result, ctime(&t)); printf("%s -> %s -> %s", argv[i], result, ctime(&t));
} }
return 0; return 0;
} }