From 229b2563dcbc2b0eb640c1dcc2c8288d09b06b81 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Wed, 9 May 2012 07:19:53 -0700 Subject: [PATCH] Display time next to change comments When the comment was posted yesterday, or any time older than 1 day but less than 1 year ago, display the time too. Display "May 2 17:37" rather than just "May 2". Bug: issue 1380 Change-Id: Ia78a20f29a0a812f0864925a094d7e225fc2631a --- .../com/google/gerrit/client/FormatUtil.java | 37 +++++++++++++++++-- .../google/gerrit/client/ui/CommentPanel.java | 2 +- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/FormatUtil.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/FormatUtil.java index e578eae473..f10762a3db 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/FormatUtil.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/FormatUtil.java @@ -25,9 +25,10 @@ import java.util.Date; public class FormatUtil { private static final long ONE_YEAR = 182L * 24 * 60 * 60 * 1000; - private static DateTimeFormat sTime = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.TIME_SHORT); - private static DateTimeFormat sDate = DateTimeFormat.getFormat("MMM d"); - private static DateTimeFormat mDate = DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_MEDIUM); + private static DateTimeFormat sTime; + private static DateTimeFormat sDate; + private static DateTimeFormat sdtFmt; + private static DateTimeFormat mDate; private static DateTimeFormat dtfmt; public static void setPreferences(AccountGeneralPreferences pref) { @@ -41,10 +42,12 @@ public class FormatUtil { } String fmt_sTime = pref.getTimeFormat().getFormat(); + String fmt_sDate = pref.getDateFormat().getShortFormat(); String fmt_mDate = pref.getDateFormat().getLongFormat(); sTime = DateTimeFormat.getFormat(fmt_sTime); - sDate = DateTimeFormat.getFormat(pref.getDateFormat().getShortFormat()); + sDate = DateTimeFormat.getFormat(fmt_sDate); + sdtFmt = DateTimeFormat.getFormat(fmt_sDate + " " + fmt_sTime); mDate = DateTimeFormat.getFormat(fmt_mDate); dtfmt = DateTimeFormat.getFormat(fmt_mDate + " " + fmt_sTime); } @@ -75,6 +78,32 @@ public class FormatUtil { } } + /** Format a date using a really short format. */ + public static String shortFormatDayTime(Date dt) { + if (dt == null) { + return ""; + } + + ensureInited(); + final Date now = new Date(); + dt = new Date(dt.getTime()); + if (mDate.format(now).equals(mDate.format(dt))) { + // Same day as today, report only the time. + // + return sTime.format(dt); + + } else if (Math.abs(now.getTime() - dt.getTime()) < ONE_YEAR) { + // Within the last year, show a shorter date. + // + return sdtFmt.format(dt); + + } else { + // Report only date and year, its far away from now. + // + return mDate.format(dt); + } + } + /** Format a date using the locale's medium length format. */ public static String mediumFormat(final Date dt) { if (dt == null) { diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/CommentPanel.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/CommentPanel.java index 0cea2c71f5..ddd2b27fb6 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/CommentPanel.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/CommentPanel.java @@ -61,7 +61,7 @@ public class CommentPanel extends Composite implements HasDoubleClickHandlers, setMessageText(message); setAuthorNameText(FormatUtil.name(author)); - setDateText(FormatUtil.shortFormat(when)); + setDateText(FormatUtil.shortFormatDayTime(when)); final CellFormatter fmt = header.getCellFormatter(); fmt.getElement(0, 0).setTitle(FormatUtil.nameEmail(author));