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

Re: [PATCH] Round-down years in "years+months" relative date view

From
Jeff King <peff@peff.net>
Date
Aug 28, 2009, 06:05 UTC
Message-ID
<20090828060538.GA22416@coredump.intra.peff.net>
In-Reply-To
<4A97193A.8090502@facebook.com>
On Thu, Aug 27, 2009 at 04:39:38PM -0700, David Reiss wrote:
> Previously, a commit from 1 year and 7 months ago would display as
> "2 years, 7 months ago".
Wow, embarrassing.
Acked-by: Jeff King <peff@peff.net>
> Here's my test script.  Let me know if you'd rather have it as part
> of the test suite.

I couldn't find any tests related to relative date processing, so it would be really nice to have some. But I'm not sure of the best way to do it without dealing with race conditions. Annoyingly, show_date calls gettimeofday at a pretty low level, so there isn't a way of instrumenting it short of LD_PRELOAD trickery (which is probably not very portable).

But maybe a patch like this is worth doing, which would allow us to test in a repeatable fashion:

---
diff --git a/date.c b/date.c
index e848d96..db2f831 100644
--- a/date.c
+++ b/date.c
@@ -86,6 +86,33 @@ static int local_tzoffset(unsigned long time)
 	return offset * eastwest;
 }
 
+static int current_time(struct timeval *now)
+{
+	static struct timeval fake_time;
+	static int use_fake_time = -1;
+
+	if (use_fake_time == -1) {
+		const char *x = getenv("GIT_FAKE_TIME");
+		if (x) {
+			char buf[50];
+			if (parse_date(x, buf, sizeof(buf)) <= 0)
+				die("unable to parse GIT_FAKE_TIME");
+			fake_time.tv_sec = strtoul(buf, NULL, 10);
+			fake_time.tv_usec = 0;
+			use_fake_time = 1;
+		}
+		else
+			use_fake_time = 0;
+	}
+
+	if (use_fake_time == 1) {
+		memcpy(now, &fake_time, sizeof(*now));
+		return 0;
+	}
+
+	return gettimeofday(now, NULL);
+}
+
 const char *show_date(unsigned long time, int tz, enum date_mode mode)
 {
 	struct tm *tm;
@@ -99,7 +126,7 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
 	if (mode == DATE_RELATIVE) {
 		unsigned long diff;
 		struct timeval now;
-		gettimeofday(&now, NULL);
+		current_time(&now);
 		if (now.tv_sec < time)
 			return "in the future";
 		diff = now.tv_sec - time;
@@ -929,7 +956,7 @@ unsigned long approxidate(const char *date)
 	if (parse_date(date, buffer, sizeof(buffer)) > 0)
 		return strtoul(buffer, NULL, 10);
 
-	gettimeofday(&tv, NULL);
+	current_time(&tv);
 	time_sec = tv.tv_sec;
 	localtime_r(&time_sec, &tm);
 	now = tm;
Previous: David ReissNext: Alex Riesen
Message 2 of 47 in “Round-down years in "years+months" relative date view”
  1. Round-down years in "years+months" relative date viewDavid Reiss, Aug 27, 2009
  2. Jeff KingAug 28, 2009
  3. Alex RiesenAug 28, 2009
  4. Jeff KingAug 28, 2009
  5. Alex RiesenAug 28, 2009
  6. Jeff KingAug 28, 2009
  7. Alex RiesenAug 28, 2009
  8. A Large Angry SCMAug 28, 2009
  9. Nicolas PitreAug 28, 2009
  10. Jeff KingAug 28, 2009
  11. Alex RiesenAug 28, 2009
  12. Jeff KingAug 28, 2009
  13. Alex RiesenAug 28, 2009
  14. Alex RiesenAug 28, 2009
  15. Nicolas PitreAug 28, 2009
  16. Alex RiesenAug 28, 2009
  17. Nicolas PitreAug 28, 2009
  18. Alex RiesenAug 28, 2009
  19. Nicolas PitreAug 28, 2009
  20. Alex RiesenAug 28, 2009
  21. Jeff KingAug 28, 2009
  22. Alex RiesenAug 28, 2009
  23. Alex RiesenAug 28, 2009
  24. Allow testing of _relative family of time formatting and parsing functionsAlex Riesen, Aug 28, 2009
  25. Alex RiesenAug 28, 2009
  26. Junio C HamanoAug 29, 2009
  27. Alex RiesenAug 30, 2009
  28. Jeff KingAug 30, 2009
  29. Alex RiesenAug 30, 2009
  30. 1/2 Add date formatting and parsing functions relative to a given timeAlex Riesen, Aug 30, 2009
  31. 2/2 Allow testing of _relative family of time formatting and parsing functionsAlex Riesen, Aug 30, 2009
  32. Jeff KingAug 30, 2009
  33. Jeff KingAug 30, 2009
  34. Alex RiesenAug 30, 2009
  35. Jeff KingAug 30, 2009
  36. Alex RiesenAug 30, 2009
  37. 1/3 Add date formatting and parsing functions relative to a given timeJeff King, Aug 30, 2009
  38. Jeff KingAug 30, 2009
  39. Jeff KingAug 31, 2009
  40. 1/4 Add date formatting and parsing functions relative to a given timeJeff King, Aug 31, 2009
  41. Alex RiesenAug 31, 2009
  42. 2/4 refactor test-date interfaceJeff King, Aug 31, 2009
  43. 3/4 tests: add date printing and parsing testsJeff King, Aug 31, 2009
  44. Jeff KingSep 1, 2009
  45. 4/4 fix approxidate parsing of relative months and yearsJeff King, Aug 31, 2009
  46. 2/3 refactor test-date interfaceJeff King, Aug 30, 2009
  47. 3/3 tests: add date printing and parsing testsJeff King, Aug 30, 2009

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.