Make date formatter available to plugins

Users can control via the user preferences how dates should be
formatted. Allow plugins to use the same date formatter as Gerrit core
that formats dates depending on the user preferences.

Note, we cannot simply move the complete FormatUtil class to
gerrit-gwtui-common because it needs access to the user preferences
and some server config, like the anonymous coward name, and this
information is retrieved differently in Gerrit core and plugins. This
is why for now we have 2 FormatUtil classes, one in Gerrit core and
one in gerrit-plugin-gwtui. Common parts such as the DateFormatter
will be made available to both FormatUtil classes by putting them into
gerrit-gwtui-common.

Change-Id: Iad241b461ad0171a2bbf279fb478dc40196cc201
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-24 16:31:26 +02:00
parent 24eea18a4c
commit ac34c132fb
3 changed files with 147 additions and 65 deletions

View File

@@ -0,0 +1,100 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.client;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gwt.i18n.client.DateTimeFormat;
import java.util.Date;
public class DateFormatter {
private final static long ONE_YEAR = 182L * 24 * 60 * 60 * 1000;
private final DateTimeFormat sTime;
private final DateTimeFormat sDate;
private final DateTimeFormat sdtFmt;
private final DateTimeFormat mDate;
private final DateTimeFormat dtfmt;
public DateFormatter(AccountPreferencesInfo prefs) {
String fmt_sTime = prefs.timeFormat().getFormat();
String fmt_sDate = prefs.dateFormat().getShortFormat();
String fmt_mDate = prefs.dateFormat().getLongFormat();
sTime = DateTimeFormat.getFormat(fmt_sTime);
sDate = DateTimeFormat.getFormat(fmt_sDate);
sdtFmt = DateTimeFormat.getFormat(fmt_sDate + " " + fmt_sTime);
mDate = DateTimeFormat.getFormat(fmt_mDate);
dtfmt = DateTimeFormat.getFormat(fmt_mDate + " " + fmt_sTime);
}
/** Format a date using a really short format. */
public String shortFormat(Date dt) {
if (dt == null) {
return "";
}
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 sDate.format(dt);
} else {
// Report only date and year, its far away from now.
//
return mDate.format(dt);
}
}
/** Format a date using a really short format. */
public String shortFormatDayTime(Date dt) {
if (dt == null) {
return "";
}
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 String mediumFormat(final Date dt) {
if (dt == null) {
return "";
}
return dtfmt.format(new Date(dt.getTime()));
}
}

View File

@@ -17,95 +17,37 @@ package com.google.gerrit.client;
import com.google.gerrit.client.info.AccountInfo; import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo; import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gwt.i18n.client.DateTimeFormat;
import java.util.Date; import java.util.Date;
/** Misc. formatting functions. */ /** Misc. formatting functions. */
public class FormatUtil { public class FormatUtil {
private static final long ONE_YEAR = 182L * 24 * 60 * 60 * 1000; private static DateFormatter dateFormatter;
private static DateTimeFormat sTime;
private static DateTimeFormat sDate;
private static DateTimeFormat sdtFmt;
private static DateTimeFormat mDate;
private static DateTimeFormat dtfmt;
public static void setPreferences(AccountPreferencesInfo prefs) { public static void setPreferences(AccountPreferencesInfo prefs) {
String fmt_sTime = prefs.timeFormat().getFormat(); dateFormatter = new DateFormatter(prefs);
String fmt_sDate = prefs.dateFormat().getShortFormat();
String fmt_mDate = prefs.dateFormat().getLongFormat();
sTime = DateTimeFormat.getFormat(fmt_sTime);
sDate = DateTimeFormat.getFormat(fmt_sDate);
sdtFmt = DateTimeFormat.getFormat(fmt_sDate + " " + fmt_sTime);
mDate = DateTimeFormat.getFormat(fmt_mDate);
dtfmt = DateTimeFormat.getFormat(fmt_mDate + " " + fmt_sTime);
} }
/** Format a date using a really short format. */ /** Format a date using a really short format. */
public static String shortFormat(Date dt) { public static String shortFormat(Date dt) {
if (dt == null) {
return "";
}
ensureInited(); ensureInited();
final Date now = new Date(); return dateFormatter.shortFormat(dt);
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 sDate.format(dt);
} else {
// Report only date and year, its far away from now.
//
return mDate.format(dt);
}
} }
/** Format a date using a really short format. */ /** Format a date using a really short format. */
public static String shortFormatDayTime(Date dt) { public static String shortFormatDayTime(Date dt) {
if (dt == null) {
return "";
}
ensureInited(); ensureInited();
final Date now = new Date(); return dateFormatter.shortFormatDayTime(dt);
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. */ /** Format a date using the locale's medium length format. */
public static String mediumFormat(final Date dt) { public static String mediumFormat(Date dt) {
if (dt == null) {
return "";
}
ensureInited(); ensureInited();
return dtfmt.format(new Date(dt.getTime())); return dateFormatter.mediumFormat(dt);
} }
private static void ensureInited() { private static void ensureInited() {
if (dtfmt == null) { if (dateFormatter == null) {
setPreferences(Gerrit.getUserPreferences()); setPreferences(Gerrit.getUserPreferences());
} }
} }

View File

@@ -0,0 +1,40 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.plugin.client;
import com.google.gerrit.client.DateFormatter;
import java.util.Date;
public class FormatUtil {
/** Format a date using a really short format. */
public static String shortFormat(Date dt) {
return createDateFormatter().shortFormat(dt);
}
/** Format a date using a really short format. */
public static String shortFormatDayTime(Date dt) {
return createDateFormatter().shortFormatDayTime(dt);
}
/** Format a date using the locale's medium length format. */
public static String mediumFormat(Date dt) {
return createDateFormatter().mediumFormat(dt);
}
private static DateFormatter createDateFormatter() {
return new DateFormatter(Plugin.get().getUserPreferences());
}
}