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

Fix approxidate() to understand more extended numbers

From
Linus Torvalds <torvalds@osdl.org>
Date
Sep 28, 2006, 19:14 UTC
Message-ID
<Pine.LNX.4.64.0609281212380.3952@g5.osdl.org>
In-Reply-To
<Pine.LNX.4.64.0609281211260.3952@g5.osdl.org>
You can now say "5:35 PM yesterday", and approxidate() gets the right answer.
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
Useful? Of course. In a sick and twisted sort of way.

Try to say "yesterday at 5 PM" before and after this. It _really_ was confused before.

diff --git a/date.c b/date.c
index 4ff6604..db4c185 100644
--- a/date.c
+++ b/date.c
@@ -598,6 +598,32 @@ static void date_tea(struct tm *tm, int 
 	date_time(tm, 17);
 }
 
+static void date_pm(struct tm *tm, int *num)
+{
+	int hour = *num;
+	*num = 0;
+
+	if (hour > 0 && hour < 12) {
+		tm->tm_hour = hour;
+		tm->tm_min = 0;
+		tm->tm_sec = 0;
+	}
+	if (tm->tm_hour > 0 && tm->tm_hour < 12)
+		tm->tm_hour += 12;
+}
+
+static void date_am(struct tm *tm, int *num)
+{
+	int hour = *num;
+	*num = 0;
+
+	if (hour > 0 && hour < 12) {
+		tm->tm_hour = hour;
+		tm->tm_min = 0;
+		tm->tm_sec = 0;
+	}
+}
+
 static const struct special {
 	const char *name;
 	void (*fn)(struct tm *, int *);
@@ -606,6 +632,8 @@ static const struct special {
 	{ "noon", date_noon },
 	{ "midnight", date_midnight },
 	{ "tea", date_tea },
+	{ "PM", date_pm },
+	{ "AM", date_am },
 	{ NULL }
 };
 
@@ -717,6 +745,18 @@ static const char *approxidate_digit(con
 	char *end;
 	unsigned long number = strtoul(date, &end, 10);
 
+	switch (*end) {
+	case ':':
+	case '.':
+	case '/':
+	case '-':
+		if (isdigit(end[1])) {
+			int match = match_multi_number(number, *end, date, end, tm);
+			if (match)
+				return date + match;
+		}
+	}
+
 	*num = number;
 	return end;
 }
Previous: Linus TorvaldsNext: Morten Welinder
Message 2 of 9 in “Clean up approxidate() in preparation for fixes”
  1. Linus TorvaldsSep 28, 2006
  2. Fix approxidate() to understand more extended numbersLinus Torvalds, Sep 28, 2006
  3. Morten WelinderSep 29, 2006
  4. Linus TorvaldsSep 29, 2006
  5. Junio C HamanoSep 29, 2006
  6. Linus TorvaldsSep 29, 2006
  7. Junio C HamanoSep 29, 2006
  8. Johannes SchindelinSep 29, 2006
  9. Linus TorvaldsSep 29, 2006

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.