Merge branch 'stable-2.14' into stable-2.15
* stable-2.14:
Fix bad rounding of relative dates like '1 year, 12 months ago'
Fix test of RelativeDateFormatter
Change-Id: I74224dd813dfcf71da98befd7ccf36973f8a6206
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java
index 7c62ed7..a0c4aa6 100644
--- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java
+++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java
@@ -14,9 +14,6 @@
package com.google.gerrit.client;
-import static com.google.gerrit.client.CommonConstants.C;
-import static com.google.gerrit.client.CommonMessages.M;
-
import java.util.Date;
/**
@@ -24,6 +21,9 @@
* defined by {@code git log --relative-date}.
*/
public class RelativeDateFormatter {
+ private static CommonConstants constants;
+ private static CommonMessages messages;
+
static final long SECOND_IN_MILLIS = 1000;
static final long MINUTE_IN_MILLIS = 60 * SECOND_IN_MILLIS;
static final long HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS;
@@ -32,6 +32,19 @@
static final long MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS;
static final long YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS;
+ static void setConstants(CommonConstants c, CommonMessages m) {
+ constants = c;
+ messages = m;
+ }
+
+ private static CommonConstants c() {
+ return constants != null ? constants : CommonConstants.C;
+ }
+
+ private static CommonMessages m() {
+ return messages != null ? messages : CommonMessages.M;
+ }
+
/**
* @param when {@link Date} to format
* @return age of given {@link Date} compared to now formatted in the same relative format as
@@ -42,81 +55,85 @@
// shouldn't happen in a perfect world
if (ageMillis < 0) {
- return C.inTheFuture();
+ return c().inTheFuture();
}
// seconds
if (ageMillis < upperLimit(MINUTE_IN_MILLIS)) {
long seconds = round(ageMillis, SECOND_IN_MILLIS);
if (seconds == 1) {
- return C.oneSecondAgo();
+ return c().oneSecondAgo();
}
- return M.secondsAgo(seconds);
+ return m().secondsAgo(seconds);
}
// minutes
if (ageMillis < upperLimit(HOUR_IN_MILLIS)) {
long minutes = round(ageMillis, MINUTE_IN_MILLIS);
if (minutes == 1) {
- return C.oneMinuteAgo();
+ return c().oneMinuteAgo();
}
- return M.minutesAgo(minutes);
+ return m().minutesAgo(minutes);
}
// hours
if (ageMillis < upperLimit(DAY_IN_MILLIS)) {
long hours = round(ageMillis, HOUR_IN_MILLIS);
if (hours == 1) {
- return C.oneHourAgo();
+ return c().oneHourAgo();
}
- return M.hoursAgo(hours);
+ return m().hoursAgo(hours);
}
// up to 14 days use days
if (ageMillis < 14 * DAY_IN_MILLIS) {
long days = round(ageMillis, DAY_IN_MILLIS);
if (days == 1) {
- return C.oneDayAgo();
+ return c().oneDayAgo();
}
- return M.daysAgo(days);
+ return m().daysAgo(days);
}
// up to 10 weeks use weeks
if (ageMillis < 10 * WEEK_IN_MILLIS) {
long weeks = round(ageMillis, WEEK_IN_MILLIS);
if (weeks == 1) {
- return C.oneWeekAgo();
+ return c().oneWeekAgo();
}
- return M.weeksAgo(weeks);
+ return m().weeksAgo(weeks);
}
// months
if (ageMillis < YEAR_IN_MILLIS) {
long months = round(ageMillis, MONTH_IN_MILLIS);
if (months == 1) {
- return C.oneMonthAgo();
+ return c().oneMonthAgo();
}
- return M.monthsAgo(months);
+ return m().monthsAgo(months);
}
// up to 5 years use "year, months" rounded to months
if (ageMillis < 5 * YEAR_IN_MILLIS) {
long years = ageMillis / YEAR_IN_MILLIS;
- String yearLabel = (years > 1) ? C.years() : C.year();
+ String yearLabel = (years > 1) ? c().years() : c().year();
long months = round(ageMillis % YEAR_IN_MILLIS, MONTH_IN_MILLIS);
- String monthLabel = (months > 1) ? C.months() : (months == 1 ? C.month() : "");
+ String monthLabel = (months > 1) ? c().months() : (months == 1 ? c().month() : "");
if (months == 0) {
- return M.years0MonthsAgo(years, yearLabel);
+ return m().years0MonthsAgo(years, yearLabel);
}
- return M.yearsMonthsAgo(years, yearLabel, months, monthLabel);
+ if (months == 12) {
+ years++;
+ return m().years0MonthsAgo(years, yearLabel);
+ }
+ return m().yearsMonthsAgo(years, yearLabel, months, monthLabel);
}
// years
long years = round(ageMillis, YEAR_IN_MILLIS);
if (years == 1) {
- return C.oneYearAgo();
+ return c().oneYearAgo();
}
- return M.yearsAgo(years);
+ return m().yearsAgo(years);
}
private static long upperLimit(long unit) {
diff --git a/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java
index 937fc96..5180410 100644
--- a/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java
+++ b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java
@@ -22,11 +22,23 @@
import static org.junit.Assert.assertEquals;
import java.util.Date;
-import org.eclipse.jgit.util.RelativeDateFormatter;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
import org.junit.Test;
public class RelativeDateFormatterTest {
+ @BeforeClass
+ public static void setConstants() {
+ Constants c = new Constants();
+ RelativeDateFormatter.setConstants(c, c);
+ }
+
+ @AfterClass
+ public static void unsetConstants() {
+ RelativeDateFormatter.setConstants(null, null);
+ }
+
private static void assertFormat(long ageFromNow, long timeUnit, String expectedFormat) {
Date d = new Date(System.currentTimeMillis() - ageFromNow * timeUnit);
String s = RelativeDateFormatter.format(d);
@@ -41,7 +53,7 @@
@Test
public void formatSeconds() {
- assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago");
+ assertFormat(1, SECOND_IN_MILLIS, "1 second ago");
assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago");
}
@@ -85,7 +97,7 @@
assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago");
assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
- assertFormat(1824, DAY_IN_MILLIS, "4 years, 12 months ago");
+ assertFormat(1824, DAY_IN_MILLIS, "5 years ago");
}
@Test
@@ -93,4 +105,111 @@
assertFormat(5, YEAR_IN_MILLIS, "5 years ago");
assertFormat(60, YEAR_IN_MILLIS, "60 years ago");
}
+
+ private static class Constants implements CommonConstants, CommonMessages {
+ @Override
+ public String inTheFuture() {
+ return "in the future";
+ }
+
+ @Override
+ public String month() {
+ return "month";
+ }
+
+ @Override
+ public String months() {
+ return "months";
+ }
+
+ @Override
+ public String year() {
+ return "year";
+ }
+
+ @Override
+ public String years() {
+ return "years";
+ }
+
+ @Override
+ public String oneSecondAgo() {
+ return "1 second ago";
+ }
+
+ @Override
+ public String oneMinuteAgo() {
+ return "1 minute ago";
+ }
+
+ @Override
+ public String oneHourAgo() {
+ return "1 hour ago";
+ }
+
+ @Override
+ public String oneDayAgo() {
+ return "1 day ago";
+ }
+
+ @Override
+ public String oneWeekAgo() {
+ return "1 week ago";
+ }
+
+ @Override
+ public String oneMonthAgo() {
+ return "1 month ago";
+ }
+
+ @Override
+ public String oneYearAgo() {
+ return "1 year ago";
+ }
+
+ @Override
+ public String secondsAgo(long seconds) {
+ return seconds + " seconds ago";
+ }
+
+ @Override
+ public String minutesAgo(long minutes) {
+ return minutes + " minutes ago";
+ }
+
+ @Override
+ public String hoursAgo(long hours) {
+ return hours + " hours ago";
+ }
+
+ @Override
+ public String daysAgo(long days) {
+ return days + " days ago";
+ }
+
+ @Override
+ public String weeksAgo(long weeks) {
+ return weeks + " weeks ago";
+ }
+
+ @Override
+ public String monthsAgo(long months) {
+ return months + " months ago";
+ }
+
+ @Override
+ public String yearsAgo(long years) {
+ return years + " years ago";
+ }
+
+ @Override
+ public String years0MonthsAgo(long years, String yearLabel) {
+ return years + " " + yearLabel + " ago";
+ }
+
+ @Override
+ public String yearsMonthsAgo(long years, String yearLabel, long months, String monthLabel) {
+ return years + " " + yearLabel + ", " + months + " " + monthLabel + " ago";
+ }
+ }
}