Skip to content

Commit 10edf37

Browse files
peffgitster
authored andcommitted
never fallback relative times to absolute
Previously, for dates older than 12 months we fell back to just giving the absolute time. This can be a bit jarring when reading a list of times. Instead, let's switch to "Y years, M months" for five years, and then just "Y years" after that. No particular reason on the 5 year cutoff except that it seemed reasonable to me. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d43c07b commit 10edf37

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

date.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,25 @@ const char *show_date(unsigned long time, int tz, enum date_mode mode)
133133
snprintf(timebuf, sizeof(timebuf), "%lu months ago", (diff + 15) / 30);
134134
return timebuf;
135135
}
136-
/* Else fall back on absolute format.. */
136+
/* Give years and months for 5 years or so */
137+
if (diff < 1825) {
138+
unsigned long years = (diff + 183) / 365;
139+
unsigned long months = (diff % 365 + 15) / 30;
140+
int n;
141+
n = snprintf(timebuf, sizeof(timebuf), "%lu year%s",
142+
years, (years > 1 ? "s" : ""));
143+
if (months)
144+
snprintf(timebuf + n, sizeof(timebuf) - n,
145+
", %lu month%s ago",
146+
months, (months > 1 ? "s" : ""));
147+
else
148+
snprintf(timebuf + n, sizeof(timebuf) - n,
149+
" ago");
150+
return timebuf;
151+
}
152+
/* Otherwise, just years. Centuries is probably overkill. */
153+
snprintf(timebuf, sizeof(timebuf), "%lu years ago", (diff + 183) / 365);
154+
return timebuf;
137155
}
138156

139157
if (mode == DATE_LOCAL)

0 commit comments

Comments
 (0)