Skip to content

Commit 81b1134

Browse files
committed
show playtime minutes if less than an hour
1 parent 42100b4 commit 81b1134

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/main/java/vc/commands/PlayerStatsCommand.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import vc.util.PlayerLookup;
1717

1818
import java.net.http.HttpTimeoutException;
19+
import java.util.ArrayList;
20+
import java.util.List;
1921

2022
import static discord4j.common.util.TimestampFormat.SHORT_DATE_TIME;
2123
import static org.slf4j.LoggerFactory.getLogger;
@@ -103,14 +105,21 @@ private String formatDuration(long durationInSeconds) {
103105
var secondsInHour = secondsInMinute * 60L;
104106
var secondsInDay = secondsInHour * 24L;
105107
var secondsInMonth = secondsInDay * 30L; // assuming 30 days per month
108+
var secondsInYear = secondsInMonth * 12L;
106109

107-
var months = durationInSeconds / secondsInMonth;
110+
var years = durationInSeconds / secondsInYear;
111+
var months = (durationInSeconds % secondsInYear) / secondsInMonth;
108112
var days = (durationInSeconds % secondsInMonth) / secondsInDay;
109113
var hours = (durationInSeconds % secondsInDay) / secondsInHour;
110-
final StringBuilder sb = new StringBuilder();
111-
sb.append((months > 0) ? months + " month" + (months != 1 ? "s" : "") + ", " : "");
112-
sb.append((days > 0) ? days + " day" + (days != 1 ? "s" : "") + ", " : "");
113-
sb.append(hours + " hour" + (hours != 1 ? "s" : ""));
114-
return sb.toString();
114+
List<String> entries = new ArrayList<>(4);
115+
if (years > 0) entries.add(years + " year" + (years != 1 ? "s" : ""));
116+
if (months > 0) entries.add(months + " month" + (months != 1 ? "s" : ""));
117+
if (days > 0) entries.add(days + " day" + (days != 1 ? "s" : ""));
118+
if (hours > 0) entries.add(hours + " hour" + (hours != 1 ? "s" : ""));
119+
if (entries.isEmpty()) {
120+
var minutes = (double) durationInSeconds / (double)secondsInMinute;
121+
return String.format("%.2f minutes", minutes);
122+
}
123+
return String.join(", ", entries);
115124
}
116125
}

src/main/java/vc/commands/PlaytimeCommand.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ private String formatDuration(long durationInSeconds) {
102102
if (months > 0) entries.add(months + " month" + (months != 1 ? "s" : ""));
103103
if (days > 0) entries.add(days + " day" + (days != 1 ? "s" : ""));
104104
if (hours > 0) entries.add(hours + " hour" + (hours != 1 ? "s" : ""));
105+
if (entries.isEmpty()) {
106+
var minutes = (double) durationInSeconds / (double)secondsInMinute;
107+
return String.format("%.2f minutes", minutes);
108+
}
105109
return String.join(", ", entries);
106110
}
107111
}

0 commit comments

Comments
 (0)