git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Teach "approxidate" about weekday syntax
@ 2005-11-17 20:36 Linus Torvalds
  2005-11-17 21:27 ` Nicolas Pitre
                   ` (2 more replies)
  0 siblings, 3 replies; 26+ messages in thread
From: Linus Torvalds @ 2005-11-17 20:36 UTC (permalink / raw)
  To: Junio C Hamano, Git Mailing List


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>
---
diff --git a/date.c b/date.c
index 73c063b..d2a67cc 100644
--- a/date.c
+++ b/date.c
@@ -34,7 +34,7 @@ static const char *month_names[] = {
 };
 
 static const char *weekday_names[] = {
-	"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
+	"Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
 };
 
 /*
@@ -531,6 +531,22 @@ static const char *approxidate_alpha(con
 		tl++;
 	}
 
+	for (i = 0; i < 7; i++) {
+		int match = match_string(date, weekday_names[i]);
+		if (match >= 3) {
+			int diff, n = *num -1;
+			*num = 0;
+
+			diff = tm->tm_wday - i;
+			if (diff <= 0)
+				n++;
+			diff += 7*n;
+
+			update_tm(tm, diff * 24 * 60 * 60);
+			return end;
+		}
+	}
+
 	if (match_string(date, "months") >= 5) {
 		int n = tm->tm_mon - *num;
 		*num = 0;

^ permalink raw reply related	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2005-11-20 23:36 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-17 20:36 Teach "approxidate" about weekday syntax Linus Torvalds
2005-11-17 21:27 ` Nicolas Pitre
2005-11-17 21:44   ` Johannes Schindelin
2005-11-17 22:54 ` Junio C Hamano
2005-11-17 23:18   ` Linus Torvalds
2005-11-17 23:23     ` Linus Torvalds
2005-11-18 12:12       ` David Roundy
2005-11-18 13:20         ` Johannes Schindelin
2005-11-18 16:08           ` Randy.Dunlap
2005-11-18 17:00             ` Linus Torvalds
2005-11-19  0:00               ` Junio C Hamano
2005-11-19  0:07                 ` Andreas Ericsson
2005-11-19  0:28                   ` Randal L. Schwartz
2005-11-19  0:48                   ` Linus Torvalds
2005-11-18 18:07             ` Matthias Urlichs
2005-11-20 23:36               ` Johannes Schindelin
2005-11-18 18:08           ` H. Peter Anvin
2005-11-18 16:56         ` Linus Torvalds
2005-11-18 17:26           ` Andreas Ericsson
2005-11-18 17:33             ` Linus Torvalds
2005-11-17 23:32     ` Johannes Schindelin
2005-11-18  1:40       ` Martin Langhoff
2005-11-18  2:38         ` Linus Torvalds
2005-11-18  2:53         ` Johannes Schindelin
2005-11-17 23:18 ` H. Peter Anvin
2005-11-17 23:30   ` Linus Torvalds

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).