Implemented Settings for Date formatting (#369)

This commit is contained in:
Rsl1122 2018-02-01 19:15:05 +02:00
parent 98d55b2ff2
commit 44cea6ff45
11 changed files with 66 additions and 59 deletions

View File

@ -72,7 +72,7 @@ public class AnalysisData extends RawData {
addValue("gmPieColors", Theme.getValue(ThemeVal.GRAPH_GM_PIE)); addValue("gmPieColors", Theme.getValue(ThemeVal.GRAPH_GM_PIE));
addValue("serverName", Settings.SERVER_NAME.toString().replaceAll("[^a-zA-Z0-9_\\s]", "_")); addValue("serverName", Settings.SERVER_NAME.toString().replaceAll("[^a-zA-Z0-9_\\s]", "_"));
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours()); addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());
addValue("refresh", FormatUtils.formatTimeStamp(refreshDate)); addValue("refresh", FormatUtils.formatTimeStampClock(refreshDate));
addValue("activityPieColors", Theme.getValue(ThemeVal.GRAPH_ACTIVITY_PIE)); addValue("activityPieColors", Theme.getValue(ThemeVal.GRAPH_ACTIVITY_PIE));
addValue("playersGraphColor", Theme.getValue(ThemeVal.GRAPH_PLAYERS_ONLINE)); addValue("playersGraphColor", Theme.getValue(ThemeVal.GRAPH_PLAYERS_ONLINE));

View File

@ -33,6 +33,7 @@ public enum Settings {
ORDER_WORLD_PIE_BY_PERC("Customization.Display.OrderWorldPieByPercentage"), ORDER_WORLD_PIE_BY_PERC("Customization.Display.OrderWorldPieByPercentage"),
PLAYERTABLE_FOOTER("Customization.Display.PlayerTableFooter"), PLAYERTABLE_FOOTER("Customization.Display.PlayerTableFooter"),
WEBSERVER_DISABLED("WebServer.DisableWebServer"), WEBSERVER_DISABLED("WebServer.DisableWebServer"),
FORMAT_DATE_RECENT_DAYS("Customization.Formatting.Dates.RecentDays"),
// Integer // Integer
WEBSERVER_PORT("WebServer.Port"), WEBSERVER_PORT("WebServer.Port"),
@ -65,6 +66,10 @@ public enum Settings {
// //
SERVER_NAME("Server.ServerName"), SERVER_NAME("Server.ServerName"),
// //
FORMAT_DATE_FULL("Customization.Formatting.Dates.Full"),
FORMAT_DATE_NO_SECONDS("Customization.Formatting.Dates.NoSeconds"),
FORMAT_DATE_CLOCK("Customization.Formatting.Dates.JustClock"),
FORMAT_DATE_RECENT_DAYS_PATTERN("Customization.Formatting.Dates.RecentDays.DatePattern"),
FORMAT_YEAR("Customization.Formatting.TimeAmount.Year"), FORMAT_YEAR("Customization.Formatting.TimeAmount.Year"),
FORMAT_YEARS("Customization.Formatting.TimeAmount.Years"), FORMAT_YEARS("Customization.Formatting.TimeAmount.Years"),
FORMAT_MONTH("Customization.Formatting.TimeAmount.Month"), FORMAT_MONTH("Customization.Formatting.TimeAmount.Month"),

View File

@ -79,7 +79,7 @@ public class InspectPage extends Page {
public String parse(PlayerProfile profile, UUID serverUUID, Map<UUID, String> serverNames) throws IOException { public String parse(PlayerProfile profile, UUID serverUUID, Map<UUID, String> serverNames) throws IOException {
long now = MiscUtils.getTime(); long now = MiscUtils.getTime();
addValue("refresh", FormatUtils.formatTimeStamp(now)); addValue("refresh", FormatUtils.formatTimeStampClock(now));
addValue("version", MiscUtils.getPlanVersion()); addValue("version", MiscUtils.getPlanVersion());
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours()); addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());

View File

@ -1,13 +1,16 @@
package com.djrapitops.plan.utilities; package com.djrapitops.plan.utilities;
import com.djrapitops.plan.system.settings.Settings; import com.djrapitops.plan.system.settings.Settings;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.utilities.Format; import com.djrapitops.plugin.utilities.Format;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.bukkit.Location; import org.bukkit.Location;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Locale;
/** /**
* @author Rsl1122 * @author Rsl1122
@ -44,16 +47,51 @@ public class FormatUtils {
return formatMilliseconds(Math.abs(after - before)); return formatMilliseconds(Math.abs(after - before));
} }
public static String formatTimeStamp(long epochMs) { public static String formatTimeStampClock(long epochMs) {
return com.djrapitops.plugin.utilities.FormatUtils.formatTimeStamp(epochMs); String format = Settings.FORMAT_DATE_CLOCK.toString();
return format(epochMs, format);
}
private static String format(long epochMs, String format) {
String locale = Settings.LOCALE.toString();
Locale usedLocale = locale.equalsIgnoreCase("default") ? Locale.ENGLISH : Locale.forLanguageTag(locale);
SimpleDateFormat dateFormat = new SimpleDateFormat(format, usedLocale);
return dateFormat.format(epochMs);
} }
public static String formatTimeStampSecond(long epochMs) { public static String formatTimeStampSecond(long epochMs) {
return com.djrapitops.plugin.utilities.FormatUtils.formatTimeStampSecond(epochMs); String format = Settings.FORMAT_DATE_FULL.toString();
if (Settings.FORMAT_DATE_RECENT_DAYS.isTrue()) {
format = replaceRecentDays(epochMs, format);
}
return format(epochMs, format);
}
private static String replaceRecentDays(long epochMs, String format) {
long now = MiscUtils.getTime();
String pattern = Settings.FORMAT_DATE_RECENT_DAYS_PATTERN.toString();
long fromStartOfDay = now % TimeAmount.DAY.ms();
if (epochMs > now - fromStartOfDay) {
format = format.replace(pattern, "'Today'");
} else if (epochMs > now - TimeAmount.DAY.ms() - fromStartOfDay) {
format = format.replace(pattern, "'Yesterday'");
} else if (epochMs > now - TimeAmount.DAY.ms() * 5L) {
format = format.replace(pattern, "EEEE");
}
return format;
} }
public static String formatTimeStampYear(long epochMs) { public static String formatTimeStampYear(long epochMs) {
return com.djrapitops.plugin.utilities.FormatUtils.formatTimeStampYear(epochMs); String format = Settings.FORMAT_DATE_NO_SECONDS.toString();
if (Settings.FORMAT_DATE_RECENT_DAYS.isTrue()) {
format = replaceRecentDays(epochMs, format);
}
return format(epochMs, format);
} }
/** /**

View File

@ -112,7 +112,7 @@ public class HtmlStructure {
int maxPlayers = properties.getMaxPlayers(); int maxPlayers = properties.getMaxPlayers();
int online = properties.getOnlinePlayers(); int online = properties.getOnlinePlayers();
Optional<Long> analysisRefreshDate = Analysis.getRefreshDate(); Optional<Long> analysisRefreshDate = Analysis.getRefreshDate();
String refresh = analysisRefreshDate.map(FormatUtils::formatTimeStamp).orElse("-"); String refresh = analysisRefreshDate.map(FormatUtils::formatTimeStampClock).orElse("-");
Server server = ServerInfo.getServer(); Server server = ServerInfo.getServer();

View File

@ -28,7 +28,7 @@ public class ActivityStackGraph extends AbstractStackGraph {
private static String[] getLabels(NavigableSet<Long> dates) { private static String[] getLabels(NavigableSet<Long> dates) {
return dates.stream() return dates.stream()
.map(FormatUtils::formatTimeStamp) .map(FormatUtils::formatTimeStampYear)
.toArray(String[]::new); .toArray(String[]::new);
} }

View File

@ -41,7 +41,7 @@ public class KillsTable extends TableContainer {
String name = dataCache.getName(kill.getVictim()); String name = dataCache.getName(kill.getVictim());
addRow( addRow(
FormatUtils.formatTimeStamp(date), FormatUtils.formatTimeStampYear(date),
Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(name), name), Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(name), name),
kill.getWeapon() kill.getWeapon()
); );

View File

@ -69,7 +69,7 @@ public class PlayersTableCreator {
playtime, FormatUtils.formatTimeAmount(playtime), playtime, FormatUtils.formatTimeAmount(playtime),
loginTimes, loginTimes,
registered, FormatUtils.formatTimeStampYear(registered), registered, FormatUtils.formatTimeStampYear(registered),
lastSeen, lastSeen != 0 ? FormatUtils.formatTimeStamp(lastSeen) : "-", lastSeen, lastSeen != 0 ? FormatUtils.formatTimeStampYear(lastSeen) : "-",
geoLocation geoLocation
)); ));
} catch (NullPointerException e) { } catch (NullPointerException e) {

View File

@ -79,7 +79,7 @@ public class SessionsTableCreator {
} }
String name = dataCache.getName(uuid); String name = dataCache.getName(uuid);
String start = FormatUtils.formatTimeStamp(session.getSessionStart()); String start = FormatUtils.formatTimeStampYear(session.getSessionStart());
String length = session.getSessionEnd() != -1 ? FormatUtils.formatTimeAmount(session.getLength()) : "Online"; String length = session.getSessionEnd() != -1 ? FormatUtils.formatTimeAmount(session.getLength()) : "Online";
String world = getLongestWorldPlayed(session); String world = getLongestWorldPlayed(session);

View File

@ -89,6 +89,17 @@ Customization:
PlayerTableFooter: true PlayerTableFooter: true
Formatting: Formatting:
DecimalPoints: '#.##' DecimalPoints: '#.##'
# Dates settings use Java SimpleDateFormat.
# You can find the patterns & examples here:
# https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
Dates:
# RecentDays replaces date with Today, Yesterday, Wednesday etc.
RecentDays: true
# Non-regex pattern to replace
DatePattern: 'MMM d YYYY'
Full: 'MMM d YYYY, HH:mm:ss'
NoSeconds: 'MMM d YYYY, HH:mm'
JustClock: 'HH:mm:ss'
TimeAmount: TimeAmount:
Year: '1 year, ' Year: '1 year, '
Years: '%years% years, ' Years: '%years% years, '

View File

@ -13,9 +13,7 @@ import utilities.RandomData;
import utilities.mocks.SystemMockUtil; import utilities.mocks.SystemMockUtil;
import utilities.mocks.objects.MockUtils; import utilities.mocks.objects.MockUtils;
import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.Locale;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -54,52 +52,7 @@ public class FormatUtilsTest {
} }
@Test @Test
public void testFormatTimeStamp() { public void testFormatTimeAmountMonths() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, HH:mm", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
String expResult = dateFormat.format(date);
long epochZero = 0L;
String result = FormatUtils.formatTimeStamp(epochZero);
assertEquals(expResult, result);
}
@Test
public void testFormatTimeStampYear() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd YYYY, HH:mm", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
String expResult = dateFormat.format(date);
long epochZero = 0L;
String result = FormatUtils.formatTimeStampYear(epochZero);
assertEquals(expResult, result);
}
@Test
public void testFormatTimeStampSecond() {
SimpleDateFormat dateFormat = new SimpleDateFormat("MMM dd, HH:mm:ss", Locale.ENGLISH);
Date date = new Date();
date.setTime(0);
String expResult = dateFormat.format(date);
long epochZero = 0L;
String result = FormatUtils.formatTimeStampSecond(epochZero);
assertEquals(expResult, result);
}
@Test
public void testFormatTimeStampMonths() {
long time = TimeAmount.DAY.ms() * 40L; long time = TimeAmount.DAY.ms() * 40L;
assertEquals("1 month, 10d ", FormatUtils.formatTimeAmount(time)); assertEquals("1 month, 10d ", FormatUtils.formatTimeAmount(time));
} }