@matheusbsbs / matheus-bagliano / commits / 9a8e35e987
Fork 0

Relative timestamps in git log

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"). [jc: with minor fix and tweak around "month" and "week" area.] Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Aug 26, 2006 at 15:45 UTC 9a8e35e98793af086f05d1ca9643052df9b44a74
4 files changed +59 -12
builtin-cat-file.c
+1 -1
@@ -54,7 +54,7 @@ static void pprint_tag(const unsigned char *sha1, const char *buf, unsigned long
54 write_or_die(1, tagger, sp - tagger);
55 date = strtoul(sp, &ep, 10);
56 tz = strtol(ep, NULL, 10);
57 - sp = show_date(date, tz);
57 + sp = show_date(date, tz, 0);
58 write_or_die(1, sp, strlen(sp));
59 xwrite(1, "\n", 1);
60 break;
cache.h
+1 -1
@@ -286,7 +286,7 @@ extern void *read_object_with_reference(const unsigned char *sha1,
286 unsigned long *size,
287 unsigned char *sha1_ret);
288
289 -const char *show_date(unsigned long time, int timezone);
289 +const char *show_date(unsigned long time, int timezone, int relative);
290 const char *show_rfc2822_date(unsigned long time, int timezone);
291 int parse_date(const char *date, char *buf, int bufsize);
292 void datestamp(char *buf, int bufsize);
commit.c
+2 -2
@@ -507,14 +507,14 @@ static int add_user_info(const char *what, enum cmit_fmt fmt, char *buf, const c
507 }
508 switch (fmt) {
509 case CMIT_FMT_MEDIUM:
510 - ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz));
510 + ret += sprintf(buf + ret, "Date: %s\n", show_date(time, tz, 0));
511 break;
512 case CMIT_FMT_EMAIL:
513 ret += sprintf(buf + ret, "Date: %s\n",
514 show_rfc2822_date(time, tz));
515 break;
516 case CMIT_FMT_FULLER:
517 - ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz));
517 + ret += sprintf(buf + ret, "%sDate: %s\n", what, show_date(time, tz, 0));
518 break;
519 default:
520 /* notin' */
date.c
+55 -8
@@ -37,6 +37,16 @@ static const char *weekday_names[] = {
37 "Sundays", "Mondays", "Tuesdays", "Wednesdays", "Thursdays", "Fridays", "Saturdays"
38 };
39
40 +static time_t gm_time_t(unsigned long time, int tz)
41 +{
42 + int minutes;
43 +
44 + minutes = tz < 0 ? -tz : tz;
45 + minutes = (minutes / 100)*60 + (minutes % 100);
46 + minutes = tz < 0 ? -minutes : minutes;
47 + return time + minutes * 60;
48 +}
49 +
50 /*
51 * The "tz" thing is passed in as this strange "decimal parse of tz"
52 * thing, which means that tz -0100 is passed in as the integer -100,
@@ -44,21 +54,58 @@ static const char *weekday_names[] = {
54 */
55 static struct tm *time_to_tm(unsigned long time, int tz)
56 {
47 - time_t t;
48 - int minutes;
49 -
50 - minutes = tz < 0 ? -tz : tz;
51 - minutes = (minutes / 100)*60 + (minutes % 100);
52 - minutes = tz < 0 ? -minutes : minutes;
53 - t = time + minutes * 60;
57 + time_t t = gm_time_t(time, tz);
58 return gmtime(&t);
59 }
60
57 -const char *show_date(unsigned long time, int tz)
61 +const char *show_date(unsigned long time, int tz, int relative)
62 {
63 struct tm *tm;
64 static char timebuf[200];
65
66 + if (relative) {
67 + unsigned long diff;
68 + time_t t = gm_time_t(time, tz);
69 + struct timeval now;
70 + gettimeofday(&now, NULL);
71 + if (now.tv_sec < t)
72 + return "in the future";
73 + diff = now.tv_sec - t;
74 + if (diff < 90) {
75 + snprintf(timebuf, sizeof(timebuf), "%lu seconds ago", diff);
76 + return timebuf;
77 + }
78 + /* Turn it into minutes */
79 + diff = (diff + 30) / 60;
80 + if (diff < 90) {
81 + snprintf(timebuf, sizeof(timebuf), "%lu minutes ago", diff);
82 + return timebuf;
83 + }
84 + /* Turn it into hours */
85 + diff = (diff + 30) / 60;
86 + if (diff < 36) {
87 + snprintf(timebuf, sizeof(timebuf), "%lu hours ago", diff);
88 + return timebuf;
89 + }
90 + /* We deal with number of days from here on */
91 + diff = (diff + 12) / 24;
92 + if (diff < 14) {
93 + snprintf(timebuf, sizeof(timebuf), "%lu days ago", diff);
94 + return timebuf;
95 + }
96 + /* Say weeks for the past 10 weeks or so */
97 + if (diff < 70) {
98 + snprintf(timebuf, sizeof(timebuf), "%lu weeks ago", (diff + 3) / 7);
99 + return timebuf;
100 + }
101 + /* Say months for the past 12 months or so */
102 + if (diff < 360) {
103 + snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
104 + return timebuf;
105 + }
106 + /* Else fall back on absolute format.. */
107 + }
108 +
109 tm = time_to_tm(time, tz);
110 if (!tm)
111 return NULL;