Format times in /lp log into a shorter form

This commit is contained in:
Luck 2017-08-19 11:38:08 +02:00
parent b65d2262bf
commit 36d292747a
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
8 changed files with 48 additions and 17 deletions

View File

@ -89,7 +89,9 @@ public class LogGroupHistory extends SubCommand<Log> {
Message.LOG_HISTORY_GROUP_HEADER.send(sender, name, page, maxPage); Message.LOG_HISTORY_GROUP_HEADER.send(sender, name, page, maxPage);
for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) { for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) {
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatDateDiff(e.getValue().getTimestamp()), e.getValue().getFormatted()); long time = e.getValue().getTimestamp();
long now = DateUtil.unixSecondsNow();
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatTimeShort(now - time), e.getValue().getFormatted());
} }
return CommandResult.SUCCESS; return CommandResult.SUCCESS;

View File

@ -142,7 +142,9 @@ public class LogRecent extends SubCommand<Log> {
} }
for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) { for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) {
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatDateDiff(e.getValue().getTimestamp()), e.getValue().getFormatted()); long time = e.getValue().getTimestamp();
long now = DateUtil.unixSecondsNow();
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatTimeShort(now - time), e.getValue().getFormatted());
} }
return CommandResult.SUCCESS; return CommandResult.SUCCESS;
} }

View File

@ -81,7 +81,9 @@ public class LogSearch extends SubCommand<Log> {
Message.LOG_SEARCH_HEADER.send(sender, query, page, maxPage); Message.LOG_SEARCH_HEADER.send(sender, query, page, maxPage);
for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) { for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) {
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatDateDiff(e.getValue().getTimestamp()), e.getValue().getFormatted()); long time = e.getValue().getTimestamp();
long now = DateUtil.unixSecondsNow();
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatTimeShort(now - time), e.getValue().getFormatted());
} }
return CommandResult.SUCCESS; return CommandResult.SUCCESS;

View File

@ -89,7 +89,9 @@ public class LogTrackHistory extends SubCommand<Log> {
Message.LOG_HISTORY_TRACK_HEADER.send(sender, name, page, maxPage); Message.LOG_HISTORY_TRACK_HEADER.send(sender, name, page, maxPage);
for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) { for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) {
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatDateDiff(e.getValue().getTimestamp()), e.getValue().getFormatted()); long time = e.getValue().getTimestamp();
long now = DateUtil.unixSecondsNow();
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatTimeShort(now - time), e.getValue().getFormatted());
} }
return CommandResult.SUCCESS; return CommandResult.SUCCESS;

View File

@ -115,7 +115,9 @@ public class LogUserHistory extends SubCommand<Log> {
Message.LOG_HISTORY_USER_HEADER.send(sender, name, page, maxPage); Message.LOG_HISTORY_USER_HEADER.send(sender, name, page, maxPage);
for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) { for (Map.Entry<Integer, LogEntry> e : entries.entrySet()) {
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatDateDiff(e.getValue().getTimestamp()), e.getValue().getFormatted()); long time = e.getValue().getTimestamp();
long now = DateUtil.unixSecondsNow();
Message.LOG_ENTRY.send(sender, e.getKey(), DateUtil.formatTimeShort(now - time), e.getValue().getFormatted());
} }
return CommandResult.SUCCESS; return CommandResult.SUCCESS;

View File

@ -74,7 +74,7 @@ public class InfoCommand extends SingleCommand {
c.get(ConfigKeys.SERVER), c.get(ConfigKeys.SERVER),
plugin.getPlayerCount(), plugin.getPlayerCount(),
plugin.getUniqueConnections().size(), plugin.getUniqueConnections().size(),
DateUtil.formatTime((System.currentTimeMillis() - plugin.getStartTime()) / 1000L), DateUtil.formatTimeShort((System.currentTimeMillis() - plugin.getStartTime()) / 1000L),
plugin.getUserManager().getAll().size(), plugin.getUserManager().getAll().size(),
plugin.getGroupManager().getAll().size(), plugin.getGroupManager().getAll().size(),
plugin.getTrackManager().getAll().size(), plugin.getTrackManager().getAll().size(),

View File

@ -158,19 +158,40 @@ public class DateUtil {
return diff; return diff;
} }
public static String formatDateDiff(long unixTime) { public static String formatDateDiff(long seconds) {
Calendar c = new GregorianCalendar();
c.setTimeInMillis(unixTime * 1000);
Calendar now = new GregorianCalendar(); Calendar now = new GregorianCalendar();
return DateUtil.formatDateDiff(now, c); Calendar then = new GregorianCalendar();
then.setTimeInMillis(seconds * 1000L);
return DateUtil.formatDateDiff(now, then);
} }
public static String formatTime(long seconds) { public static String formatTimeShort(long seconds) {
Calendar c = new GregorianCalendar(); if (seconds == 0) {
c.setTimeInMillis(0L); return "0s";
Calendar c2 = new GregorianCalendar(); }
c2.setTimeInMillis(seconds * 1000L);
return DateUtil.formatDateDiff(c, c2); long minute = seconds / 60;
seconds = seconds % 60;
long hour = minute / 60;
minute = minute % 60;
long day = hour / 24;
hour = hour % 24;
StringBuilder time = new StringBuilder();
if (day != 0) {
time.append(day).append("d ");
}
if (hour != 0) {
time.append(hour).append("h ");
}
if (minute != 0) {
time.append(minute).append("m ");
}
if (seconds != 0) {
time.append(seconds).append("s");
}
return time.toString().trim();
} }
private static String formatDateDiff(Calendar fromDate, Calendar toDate) { private static String formatDateDiff(Calendar fromDate, Calendar toDate) {

View File

@ -113,7 +113,7 @@ public class VerboseListener {
String startDate = DATE_FORMAT.format(new Date(startTime)); String startDate = DATE_FORMAT.format(new Date(startTime));
String endDate = DATE_FORMAT.format(new Date(now)); String endDate = DATE_FORMAT.format(new Date(now));
long secondsTaken = (now - startTime) / 1000L; long secondsTaken = (now - startTime) / 1000L;
String duration = DateUtil.formatTime(secondsTaken); String duration = DateUtil.formatTimeShort(secondsTaken);
String filter = this.filter; String filter = this.filter;
if (filter == null || filter.equals("")){ if (filter == null || filter.equals("")){