threads / discuss / 5402

Relative timestamps in git log

Subject: Relative timestamps in git log

## tl;dr

2 messages between Aug 26, 2006 and Aug 27, 2006.

replies: 1people: 2as markdown or json

Linus Torvalds· Aug 26, 2006, 22:45 UTC · lore

I noticed that I was looking at the kernel gitweb output at some point rather than just do "git log", simply because I liked seeing the simplified date-format, ie the "5 days ago" rather than a full date.

This adds infrastructure to do that for "git log" too. It does NOT add the actual flag to enable it, though, so right now this patch is a no-op, but it should now be easy to add a command line flag (and possibly a config file option) to just turn on the "relative" date format.

The exact cut-off points when it switches from days to weeks etc are totally arbitrary, but are picked somewhat to avoid the "1 weeks ago" thing (by making it show "10 days ago" rather than "1 week", or "70 minutes ago" rather than "1 hour ago").

Signed-off-by: Linus Torvalds <torvalds@osdl.org>
---
With this, and changing the ", 0" for CMIT_FMT_MEDIUM into a ", 1", I get 
output like:
	commit a7f051987c5f020e60da1e5d6ddefc3d443d3299
	Merge: 030b520... a8e0d16...
	Author: Junio C Hamano <junkio@cox.net>
	Date:   22 hours ago
	
	    Merge branch 'gl/cleanup'
	    
	    * gl/cleanup:
	      Convert memset(hash,0,20) to hashclr(hash).
	      Convert memcpy(a,b,20) to hashcpy(a,b).
	
	commit 030b52087f0a4b7b1178d34839868ce438eb2f0e
	Author: Jakub Narebski <jnareb@gmail.com>
	Date:   20 hours ago
	
	    gitweb: git_annotate didn't expect negative numeric timezone
	    
	    Signed-off-by: Jakub Narebski <jnareb@gmail.com>
	    Signed-off-by: Junio C Hamano <junkio@cox.net>
	
	commit b22d449721b22f6ec090f22c418ae6b0a560f78d
	Author: Eric Wong <normalperson@yhbt.net>
	Date:   23 hours ago
	
	    git-svn: add the 'dcommit' command
	    
	    This is a high-level wrapper around the 'commit-diff' command
	    and used to produce cleaner history against the mirrored repository
	    through rebase/reset usage.
	    
	    It's basically a more polished version of this:
	    
	    for i in `git rev-list --no-merges remotes/git-svn..HEAD | tac`; do
	        git-svn commit-diff $i~1 $i
	    done
	    git reset --hard remotes/git-svn
	    
	    Signed-off-by: Eric Wong <normalperson@yhbt.net>
	    Signed-off-by: Junio C Hamano <junkio@cox.net>
which looks quite readable..
diff --git a/builtin-cat-file.c b/builtin-cat-file.c
index 7a6fa56..6c16bfa 100644
--- a/builtin-cat-file.c
+++ b/builtin-cat-file.c
@@ -54,7 +54,7 @@ static void pprint_tag(const unsigned ch
 					write_or_die(1, tagger, sp - tagger);
 					date = strtoul(sp, &ep, 10);
 					tz = strtol(ep, NULL, 10);
-					sp = show_date(date, tz);
+					sp = show_date(date, tz, 0);
 					write_or_die(1, sp, strlen(sp));
 					xwrite(1, "\n", 1);
 					break;
diff --git a/cache.h b/cache.h
index 1f212d7..ccb83a1 100644
--- a/cache.h
+++ b/cache.h
@@ -286,7 +286,7 @@ extern void *read_object_with_reference(
 					unsigned long *size,
 					unsigned char *sha1_ret);
 
-const char *show_date(unsigned long time, int timezone);
+const char *show_date(unsigned long time, int timezone, int relative);
 const char *show_rfc2822_date(unsigned long time, int timezone);
 int parse_date(const char *date, char *buf, int bufsize);
 void datestamp(char *buf, int bufsize);
diff --git a/commit.c b/commit.c
index 00bc3de..c3ff9b4 100644
--- a/commit.c
+++ b/commit.c
@@ -507,14 +507,14 @@ static int add_user_info(const char *wha
 	}
 	switch (fmt) {
 	case CMIT_FMT_MEDIUM:
-		ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz));
+		ret += sprintf(buf + ret, "Date:   %s\n", show_date(time, tz, 0));
 		break;
 	case CMIT_FMT_EMAIL:
 		ret += sprintf(buf + ret, "Date: %s\n",
 			       show_rfc2822_date(time, tz));
 		break;
 	case CMIT_FMT_FULLER:
-		ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
+		ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz, 0));
 		break;
 	default:
 		/* notin' */
diff --git a/date.c b/date.c
index d780846..92e3f6e 100644
--- a/date.c
+++ b/date.c
@@ -37,6 +37,16 @@ static const char *weekday_names[] = {
 	"Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
 };
 
+static time_t gm_time_t(unsigned long time, int tz)
+{
+	int minutes;
+
+	minutes = tz < 0 ? -tz : tz;
+	minutes = (minutes / 100)*60 + (minutes % 100);
+	minutes = tz < 0 ? -minutes : minutes;
+	return time + minutes * 60;
+}
+
 /*
  * The "tz" thing is passed in as this strange "decimal parse of tz"
  * thing, which means that tz -0100 is passed in as the integer -100,
@@ -44,21 +54,56 @@ static const char *weekday_names[] = {
  */
 static struct tm *time_to_tm(unsigned long time, int tz)
 {
-	time_t t;
-	int minutes;
-
-	minutes = tz < 0 ? -tz : tz;
-	minutes = (minutes / 100)*60 + (minutes % 100);
-	minutes = tz < 0 ? -minutes : minutes;
-	t = time + minutes * 60;
+	time_t t = gm_time_t(time, tz);
 	return gmtime(&t);
 }
 
-const char *show_date(unsigned long time, int tz)
+const char *show_date(unsigned long time, int tz, int relative)
 {
 	struct tm *tm;
 	static char timebuf[200];
 
+	if (relative) {
+		unsigned long diff;
+		time_t t = gm_time_t(time, tz);
+		struct timeval now;
+		gettimeofday(&now, NULL);
+		if (now.tv_sec < t)
+			return "in the future";
+		diff = now.tv_sec - t;
+		if (diff < 90) {
+			snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
+			return timebuf;
+		}
+		/* Turn it into minutes */
+		diff = (diff + 30) / 60;
+		if (diff < 90) {
+			snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
+			return timebuf;
+		}
+		/* Turn it into hours */
+		diff = (diff + 30) / 60;
+		if (diff < 36) {
+			snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
+			return timebuf;
+		}
+		/* Turn it into days */
+		diff = (diff + 12) / 24;
+		if (diff < 14) {
+			snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
+			return timebuf;
+		}
+		if (diff < 53) {
+			snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
+			return timebuf;
+		}
+		if (diff < 365) {
+			snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
+			return timebuf;
+		}
+		/* Else fall back on absolute format.. */
+	}
+
 	tm = time_to_tm(time, tz);
 	if (!tm)
 		return NULL;
Junio C Hamano· Aug 27, 2006, 01:22 UTC · re: Linus Torvalds · lore

Re: Relative timestamps in git log

Linus Torvalds <torvalds@osdl.org> writes:
> The exact cut-off points when it switches from days to weeks etc are 
> totally arbitrary, but are picked somewhat to avoid the "1 weeks ago" 
> thing (by making it show "10 days ago" rather than "1 week", or "70 
> minutes ago" rather than "1 hour ago").
It might be arbitrary but I agree they make sense.  

The code was quite confusing in that after you turn it into days you stop converting to larger scale and I was going to nitpick that 365 weeks do not make much sense. I think you meant to do a "Turn it into weeks" conversion before comparing diff with 53 and then changed your mind, got confused yourself that diff is still in days and decided to say "months" for the past year.

IOW, I think there is a bug around weeks/months code ;-). On top of your patch how about the attached?

Personally I have mixed feeling about "months" scale. On one hand, I think it could extend to 30 months or so without losing readability when one is interested in "how long ago did this happen". On the other hand, it would take more time for me if I see "this happened 5 months ago" than "this happened in March this year" to recollect what the context of the particular change was ("ah I needed that feature when I was preparing the committer/author graph for OLS paper deadline").

P.S. welcome back to the list.
diff --git a/date.c b/date.c
index 92e3f6e..914fd82 100644
--- a/date.c
+++ b/date.c
@@ -87,17 +87,19 @@ const char *show_date(unsigned long time
 			snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
 			return timebuf;
 		}
-		/* Turn it into days */
+		/* Turn it into days; from here on we deal with days */
 		diff = (diff + 12) / 24;
 		if (diff < 14) {
 			snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
 			return timebuf;
 		}
-		if (diff < 53) {
+		/* Say weeks for the past 6 months or so */
+		if (diff < 180) {
 			snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
 			return timebuf;
 		}
-		if (diff < 365) {
+		/* Say months for the past 30 months or so */
+		if (diff < 912) {
 			snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
 			return timebuf;
 		}

← back to recent threads