@matheusbsbs / matheus-bagliano / commits / 6b7b042772
Fork 0

Teach "approxidate" about weekday syntax

This allows people to use syntax like "last thursday" for the approxidate. (Or, indeed, more complex "three thursdays ago", but I suspect that would be pretty unusual). NOTE! The parsing is strictly sequential, so if you do "one day before last thursday" it will _not_ do what you think it does. It will take the current time, subtract one day, and then go back to the thursday before that. So to get what you want, you'd have to write it the other way around: "last thursday and one day before" which is insane (it's usually the same as "last wednesday" _except_ if today is Thursday, in which case "last wednesday" is yesterday, and "last thursday and one day before" is eight days ago). Similarly, "last thursday one month ago" will first go back to last thursday, and then go back one month from there, not the other way around. I doubt anybody would ever use insane dates like that, but I thought I'd point out that the approxidate parsing is not exactly "standard English". Side note 2: if you want to avoid spaces (because of quoting issues), you can use any non-alphanumberic character instead. So git log --since=2.days.ago works without any quotes. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Nov 17, 2005 at 12:36 UTC 6b7b0427728fe31ee2d9375a36f1c27974432979
1 file changed +17 -1
date.c
+17 -1
@@ -34,7 +34,7 @@ static const char *month_names[] = {
34 };
35
36 static const char *weekday_names[] = {
37 - "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
37 + "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
38 };
39
40 /*
@@ -531,6 +531,22 @@ static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
531 tl++;
532 }
533
534 + for (i = 0; i < 7; i++) {
535 + int match = match_string(date, weekday_names[i]);
536 + if (match >= 3) {
537 + int diff, n = *num -1;
538 + *num = 0;
539 +
540 + diff = tm->tm_wday - i;
541 + if (diff <= 0)
542 + n++;
543 + diff += 7*n;
544 +
545 + update_tm(tm, diff * 24 * 60 * 60);
546 + return end;
547 + }
548 + }
549 +
550 if (match_string(date, "months") >= 5) {
551 int n = tm->tm_mon - *num;
552 *num = 0;