aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael J Gruber <git@drmicha.warpmail.net>2011-04-20 11:12:11 +0200
committerJunio C Hamano <gitster@pobox.com>2011-04-20 19:23:16 -0700
commitf1e9c548ce45005521892af0299696204ece286b (patch)
tree6387f371b2dcb5c837325b661aa04322d5c5863f
parent87b50542a08ac6caa083ddc376e674424e37940a (diff)
downloadgit-f1e9c548ce45005521892af0299696204ece286b.tar.gz
git-f1e9c548ce45005521892af0299696204ece286b.tar.xz
date: avoid "X years, 12 months" in relative dates
When relative dates are more than about a year ago, we start writing them as "Y years, M months". At the point where we calculate Y and M, we have the time delta specified as a number of days. We calculate these integers as: Y = days / 365 M = (days % 365 + 15) / 30 This rounds days in the latter half of a month up to the nearest month, so that day 16 is "1 month" (or day 381 is "1 year, 1 month"). We don't round the year at all, though, meaning we can end up with "1 year, 12 months", which is silly; it should just be "2 years". Implement this differently with months of size onemonth = 365/12 so that totalmonths = (long)( (days + onemonth/2)/onemonth ) years = totalmonths / 12 months = totalmonths % 12 In order to do this without floats, we write the first formula as totalmonths = (days*12*2 + 365) / (365*2) Tests and inspiration by Jeff King. Helped-by: Jeff King <peff@peff.net> Signed-off-by: Michael J Gruber <git@drmicha.warpmail.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r--date.c5
-rwxr-xr-xt/t0006-date.sh1
2 files changed, 4 insertions, 2 deletions
diff --git a/date.c b/date.c
index 00f9eb5d0..896fbb480 100644
--- a/date.c
+++ b/date.c
@@ -129,8 +129,9 @@ const char *show_date_relative(unsigned long time, int tz,
}
/* Give years and months for 5 years or so */
if (diff < 1825) {
- unsigned long years = diff / 365;
- unsigned long months = (diff % 365 + 15) / 30;
+ unsigned long totalmonths = (diff * 12 * 2 + 365) / (365 * 2);
+ unsigned long years = totalmonths / 12;
+ unsigned long months = totalmonths % 12;
int n;
n = snprintf(timebuf, timebuf_size, "%lu year%s",
years, (years > 1 ? "s" : ""));
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
index 1d4d0a5c7..f87abb5a0 100755
--- a/t/t0006-date.sh
+++ b/t/t0006-date.sh
@@ -25,6 +25,7 @@ check_show 37500000 '1 year, 2 months ago'
check_show 55188000 '1 year, 9 months ago'
check_show 630000000 '20 years ago'
check_show 31449600 '12 months ago'
+check_show 62985600 '2 years ago'
check_parse() {
echo "$1 -> $2" >expect