mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2024-12-28 12:07:35 +01:00
Implemented Settings for Date formatting (#369)
This commit is contained in:
parent
98d55b2ff2
commit
44cea6ff45
@ -72,7 +72,7 @@ public class AnalysisData extends RawData {
|
||||
addValue("gmPieColors", Theme.getValue(ThemeVal.GRAPH_GM_PIE));
|
||||
addValue("serverName", Settings.SERVER_NAME.toString().replaceAll("[^a-zA-Z0-9_\\s]", "_"));
|
||||
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());
|
||||
addValue("refresh", FormatUtils.formatTimeStamp(refreshDate));
|
||||
addValue("refresh", FormatUtils.formatTimeStampClock(refreshDate));
|
||||
|
||||
addValue("activityPieColors", Theme.getValue(ThemeVal.GRAPH_ACTIVITY_PIE));
|
||||
addValue("playersGraphColor", Theme.getValue(ThemeVal.GRAPH_PLAYERS_ONLINE));
|
||||
|
@ -33,6 +33,7 @@ public enum Settings {
|
||||
ORDER_WORLD_PIE_BY_PERC("Customization.Display.OrderWorldPieByPercentage"),
|
||||
PLAYERTABLE_FOOTER("Customization.Display.PlayerTableFooter"),
|
||||
WEBSERVER_DISABLED("WebServer.DisableWebServer"),
|
||||
FORMAT_DATE_RECENT_DAYS("Customization.Formatting.Dates.RecentDays"),
|
||||
|
||||
// Integer
|
||||
WEBSERVER_PORT("WebServer.Port"),
|
||||
@ -65,6 +66,10 @@ public enum Settings {
|
||||
//
|
||||
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_YEARS("Customization.Formatting.TimeAmount.Years"),
|
||||
FORMAT_MONTH("Customization.Formatting.TimeAmount.Month"),
|
||||
|
@ -79,7 +79,7 @@ public class InspectPage extends Page {
|
||||
public String parse(PlayerProfile profile, UUID serverUUID, Map<UUID, String> serverNames) throws IOException {
|
||||
long now = MiscUtils.getTime();
|
||||
|
||||
addValue("refresh", FormatUtils.formatTimeStamp(now));
|
||||
addValue("refresh", FormatUtils.formatTimeStampClock(now));
|
||||
addValue("version", MiscUtils.getPlanVersion());
|
||||
addValue("timeZone", MiscUtils.getTimeZoneOffsetHours());
|
||||
|
||||
|
@ -1,13 +1,16 @@
|
||||
package com.djrapitops.plan.utilities;
|
||||
|
||||
import com.djrapitops.plan.system.settings.Settings;
|
||||
import com.djrapitops.plugin.api.TimeAmount;
|
||||
import com.djrapitops.plugin.utilities.Format;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* @author Rsl1122
|
||||
@ -44,16 +47,51 @@ public class FormatUtils {
|
||||
return formatMilliseconds(Math.abs(after - before));
|
||||
}
|
||||
|
||||
public static String formatTimeStamp(long epochMs) {
|
||||
return com.djrapitops.plugin.utilities.FormatUtils.formatTimeStamp(epochMs);
|
||||
public static String formatTimeStampClock(long 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) {
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,7 +112,7 @@ public class HtmlStructure {
|
||||
int maxPlayers = properties.getMaxPlayers();
|
||||
int online = properties.getOnlinePlayers();
|
||||
Optional<Long> analysisRefreshDate = Analysis.getRefreshDate();
|
||||
String refresh = analysisRefreshDate.map(FormatUtils::formatTimeStamp).orElse("-");
|
||||
String refresh = analysisRefreshDate.map(FormatUtils::formatTimeStampClock).orElse("-");
|
||||
|
||||
Server server = ServerInfo.getServer();
|
||||
|
||||
|
@ -28,7 +28,7 @@ public class ActivityStackGraph extends AbstractStackGraph {
|
||||
|
||||
private static String[] getLabels(NavigableSet<Long> dates) {
|
||||
return dates.stream()
|
||||
.map(FormatUtils::formatTimeStamp)
|
||||
.map(FormatUtils::formatTimeStampYear)
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class KillsTable extends TableContainer {
|
||||
|
||||
String name = dataCache.getName(kill.getVictim());
|
||||
addRow(
|
||||
FormatUtils.formatTimeStamp(date),
|
||||
FormatUtils.formatTimeStampYear(date),
|
||||
Html.LINK.parse(PlanAPI.getInstance().getPlayerInspectPageLink(name), name),
|
||||
kill.getWeapon()
|
||||
);
|
||||
|
@ -69,7 +69,7 @@ public class PlayersTableCreator {
|
||||
playtime, FormatUtils.formatTimeAmount(playtime),
|
||||
loginTimes,
|
||||
registered, FormatUtils.formatTimeStampYear(registered),
|
||||
lastSeen, lastSeen != 0 ? FormatUtils.formatTimeStamp(lastSeen) : "-",
|
||||
lastSeen, lastSeen != 0 ? FormatUtils.formatTimeStampYear(lastSeen) : "-",
|
||||
geoLocation
|
||||
));
|
||||
} catch (NullPointerException e) {
|
||||
|
@ -79,7 +79,7 @@ public class SessionsTableCreator {
|
||||
}
|
||||
|
||||
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 world = getLongestWorldPlayed(session);
|
||||
|
||||
|
@ -89,6 +89,17 @@ Customization:
|
||||
PlayerTableFooter: true
|
||||
Formatting:
|
||||
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:
|
||||
Year: '1 year, '
|
||||
Years: '%years% years, '
|
||||
|
@ -13,9 +13,7 @@ import utilities.RandomData;
|
||||
import utilities.mocks.SystemMockUtil;
|
||||
import utilities.mocks.objects.MockUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@ -54,52 +52,7 @@ public class FormatUtilsTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFormatTimeStamp() {
|
||||
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() {
|
||||
public void testFormatTimeAmountMonths() {
|
||||
long time = TimeAmount.DAY.ms() * 40L;
|
||||
assertEquals("1 month, 10d ", FormatUtils.formatTimeAmount(time));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user