1 package net.pterodactylus.xdcc.ui.stdin;
3 import java.io.IOException;
5 import java.time.Duration;
6 import java.time.OffsetDateTime;
7 import java.time.format.DateTimeFormatter;
8 import java.util.Collection;
9 import java.util.Collections;
10 import java.util.List;
11 import java.util.stream.Collectors;
12 import java.util.stream.Stream;
14 import net.pterodactylus.xdcc.core.Core;
15 import net.pterodactylus.xdcc.data.ConnectedNetwork;
18 * {@link Command} that writes uptime information.
20 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
22 public class UptimeCommand implements Command {
24 private final DateTimeFormatter formatter = DateTimeFormatter.RFC_1123_DATE_TIME;
25 private final Core core;
27 public UptimeCommand(Core core) {
32 public String getName() {
37 public Collection<String> getAliases() {
38 return Collections.emptySet();
42 public State execute(State state, List<String> parameters, Writer outputWriter)
44 OffsetDateTime now = OffsetDateTime.now();
45 outputWriter.write(String.format("Core running since %s, %s ago.\n",
46 formatter.format(core.getUptime().subtractFrom(now)),
47 formatTime(core.getUptime())));
48 for (ConnectedNetwork network : core.connectedNetworks()) {
49 outputWriter.write(String.format("%s connected since %s, %s ago.\n",
50 network.getNetwork().name(),
51 formatter.format(network.getUptime().subtractFrom(now)),
52 formatTime(network.getUptime())));
57 private String formatTime(Duration uptime) {
58 long uptimeSeconds = uptime.getSeconds();
59 long seconds = uptimeSeconds % 60;
60 long minutes = (uptimeSeconds / 60) % 60;
61 long hours = (uptimeSeconds / (60 * 60)) % 24;
62 long days = (uptimeSeconds / (60 * 60 * 24)) % 7;
63 long weeks = (uptimeSeconds / (60 * 60 * 24 * 7));
64 String uptimeString = Stream
65 .of(weeks + "w", days + "d", hours + "h", minutes + "m", seconds + "s")
66 .filter(v -> !v.startsWith("0"))
67 .collect(Collectors.joining(" "));
68 return uptimeString.isEmpty() ? "0s" : uptimeString;