git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Teach "approxidate" about weekday syntax

From
Linus Torvalds <torvalds@osdl.org>
Date
Nov 17, 2005, 20:36 UTC
Message-ID
<Pine.LNX.4.64.0511171223110.13959@g5.osdl.org>
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;
Next: Nicolas Pitre
Message 1 of 26 in “Teach "approxidate" about weekday syntax”
  1. Linus TorvaldsNov 17, 2005
  2. Nicolas PitreNov 17, 2005
  3. Johannes SchindelinNov 17, 2005
  4. Junio C HamanoNov 17, 2005
  5. Linus TorvaldsNov 17, 2005
  6. Linus TorvaldsNov 17, 2005
  7. David RoundyNov 18, 2005
  8. Johannes SchindelinNov 18, 2005
  9. Randy.DunlapNov 18, 2005
  10. Linus TorvaldsNov 18, 2005
  11. Junio C HamanoNov 19, 2005
  12. Andreas EricssonNov 19, 2005
  13. Randal L. SchwartzNov 19, 2005
  14. Linus TorvaldsNov 19, 2005
  15. Matthias UrlichsNov 18, 2005
  16. Johannes SchindelinNov 20, 2005
  17. H. Peter AnvinNov 18, 2005
  18. Linus TorvaldsNov 18, 2005
  19. Andreas EricssonNov 18, 2005
  20. Linus TorvaldsNov 18, 2005
  21. Johannes SchindelinNov 17, 2005
  22. Martin LanghoffNov 18, 2005
  23. Linus TorvaldsNov 18, 2005
  24. Johannes SchindelinNov 18, 2005
  25. H. Peter AnvinNov 17, 2005
  26. Linus TorvaldsNov 17, 2005

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.