RelativeDateFormatter: Fix bad rounding of "1 year 12 months" to "2 year"

Change I8b1af0e72 fixed rounding of dates, but it only works properly
when the year is already greater than 1.

In the case "1 year 12 months", the year is correctly incremented to 2,
but the label is not adjusted, resulting in "2 year".

This was not noticed during review of I8b1af0e72 because the test that
was added only tests for "4 years 12 months" being rounded to "5 years".

Add an adjustment of the label when the year count is incremented, and
add a test for "1 year 12 months" being correctly rounded to "2 years".

The test is taken from [1] on the jgit project.

[1] https://git.eclipse.org/r/#/c/109895/

Bug: Issue 7357
Change-Id: I9729e6ad82ad31f36d889f59a6663c7c05495b73
This commit is contained in:
David Pursehouse
2017-10-13 11:19:50 +09:00
parent d57189eb4f
commit 1b596afc4d
2 changed files with 4 additions and 0 deletions

View File

@@ -123,6 +123,9 @@ public class RelativeDateFormatter {
}
if (months == 12) {
years++;
if (years > 1) {
yearLabel = c().years();
}
return m().years0MonthsAgo(years, yearLabel);
}
return m().yearsMonthsAgo(years, yearLabel, months, monthLabel);

View File

@@ -98,6 +98,7 @@ public class RelativeDateFormatterTest {
assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
assertFormat(1824, DAY_IN_MILLIS, "5 years ago");
assertFormat(2 * 365 - 10, DAY_IN_MILLIS, "2 years ago");
}
@Test