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

Re: Teach "approxidate" about weekday syntax

From
Linus Torvalds <torvalds@osdl.org>
Date
Nov 18, 2005, 16:56 UTC
Message-ID
<Pine.LNX.4.64.0511180842480.13959@g5.osdl.org>
In-Reply-To
<20051118121210.GA19714@abridgegame.org>
On Fri, 18 Nov 2005, David Roundy wrote:
> 
> Don't forget "high noon"!  (and perhaps "tea time"?)  :)
Done.
    [torvalds@g5 git]$ ./test-date "now" "midnight" "high noon" "tea-time"
    now -> bad -> Wed Dec 31 16:00:00 1969
    now -> Fri Nov 18 08:50:54 2005
    midnight -> bad -> Wed Dec 31 16:00:00 1969
    midnight -> Fri Nov 18 00:00:00 2005
    high noon -> bad -> Wed Dec 31 16:00:00 1969
    high noon -> Thu Nov 17 12:00:00 2005
    tea-time -> bad -> Wed Dec 31 16:00:00 1969
    tea-time -> Thu Nov 17 17:00:00 2005
Thanks for pointing out tea-time.

This is also written to easily extended to allow people to add their own important dates like Christmas and their own birthdays.

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
diff --git a/date.c b/date.c
index d2a67cc..3e11500 100644
--- a/date.c
+++ b/date.c
@@ -468,12 +468,52 @@ static void update_tm(struct tm *tm, uns
 	localtime_r(&n, tm);
 }
 
+static void date_yesterday(struct tm *tm, int *num)
+{
+	update_tm(tm, 24*60*60);
+}
+
+static void date_time(struct tm *tm, int hour)
+{
+	if (tm->tm_hour < hour)
+		date_yesterday(tm, NULL);
+	tm->tm_hour = hour;
+	tm->tm_min = 0;
+	tm->tm_sec = 0;
+}
+
+static void date_midnight(struct tm *tm, int *num)
+{
+	date_time(tm, 0);
+}
+
+static void date_noon(struct tm *tm, int *num)
+{
+	date_time(tm, 12);
+}
+
+static void date_tea(struct tm *tm, int *num)
+{
+	date_time(tm, 17);
+}
+
+static const struct special {
+	const char *name;
+	void (*fn)(struct tm *, int *);
+} special[] = {
+	{ "yesterday", date_yesterday },
+	{ "noon", date_noon },
+	{ "midnight", date_midnight },
+	{ "tea", date_tea },
+	{ NULL }
+};
+
 static const char *number_name[] = {
 	"zero", "one", "two", "three", "four",
 	"five", "six", "seven", "eight", "nine", "ten",
 };
 
-static struct typelen {
+static const struct typelen {
 	const char *type;
 	int length;
 } typelen[] = {
@@ -487,7 +527,8 @@ static struct typelen {
 
 static const char *approxidate_alpha(const char *date, struct tm *tm, int *num)
 {
-	struct typelen *tl;
+	const struct typelen *tl;
+	const struct special *s;
 	const char *end = date;
 	int n = 1, i;
 
@@ -502,9 +543,12 @@ static const char *approxidate_alpha(con
 		}
 	}
 
-	if (match_string(date, "yesterday") > 8) {
-		update_tm(tm, 24*60*60);
-		return end;
+	for (s = special; s->name; s++) {
+		int len = strlen(s->name);
+		if (match_string(date, s->name) == len) {
+			s->fn(tm, num);
+			return end;
+		}
 	}
 
 	if (!*num) {
Previous: H. Peter AnvinNext: Andreas Ericsson
Message 18 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.