mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI.git
synced 2024-11-29 14:06:33 +01:00
Refactorization of the TimeUtil utility class (#423)
* Refactored `TimeUtil` utility class. * Minimized `TimeUtil#getRemaining`. * Fixed a bug regarding how days and hours were calculated in `TimeUtil#getTime`. * Ditched the use of `int`s and use `long`s instead. * Implemented `int`-taking methods because my dumb ass didn't think it would break.
This commit is contained in:
parent
722e987b93
commit
8698449e5d
@ -22,100 +22,31 @@ package me.clip.placeholderapi.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class TimeUtil {
|
||||
|
||||
public static String getRemaining(int seconds, TimeFormat type) {
|
||||
if (seconds < 60) {
|
||||
public static String getRemaining(final int seconds, final TimeFormat type) {
|
||||
return getRemaining((long) seconds, type);
|
||||
}
|
||||
|
||||
public static String getRemaining(final long seconds, final TimeFormat type) {
|
||||
switch (type) {
|
||||
case DAYS:
|
||||
case HOURS:
|
||||
case MINUTES:
|
||||
return "0";
|
||||
default:
|
||||
return String.valueOf(seconds);
|
||||
|
||||
case SECONDS:
|
||||
return String.valueOf(seconds);
|
||||
}
|
||||
return String.valueOf(seconds % 60);
|
||||
|
||||
return String.valueOf(seconds);
|
||||
}
|
||||
|
||||
int minutes = seconds / 60;
|
||||
int s = 60 * minutes;
|
||||
int secondsLeft = seconds - s;
|
||||
|
||||
if (minutes < 60) {
|
||||
switch (type) {
|
||||
case DAYS:
|
||||
case HOURS:
|
||||
return "0";
|
||||
case MINUTES:
|
||||
return String.valueOf(minutes);
|
||||
case SECONDS:
|
||||
return String.valueOf(secondsLeft);
|
||||
}
|
||||
return String.valueOf((seconds / 60) % 60);
|
||||
|
||||
return String.valueOf(seconds);
|
||||
}
|
||||
|
||||
if (minutes < 1440) {
|
||||
int hours = minutes / 60;
|
||||
int inMins = 60 * hours;
|
||||
int leftOver = minutes - inMins;
|
||||
|
||||
switch (type) {
|
||||
case DAYS:
|
||||
return "0";
|
||||
case HOURS:
|
||||
return String.valueOf(hours);
|
||||
case MINUTES:
|
||||
return String.valueOf(leftOver);
|
||||
case SECONDS:
|
||||
return String.valueOf(secondsLeft);
|
||||
}
|
||||
return String.valueOf((seconds / 3600) % 24);
|
||||
|
||||
return String.valueOf(seconds);
|
||||
}
|
||||
|
||||
int days = minutes / 1440;
|
||||
int inMins = 1440 * days;
|
||||
int leftOver = minutes - inMins;
|
||||
|
||||
if (leftOver < 60) {
|
||||
switch (type) {
|
||||
case DAYS:
|
||||
return String.valueOf(days);
|
||||
case HOURS:
|
||||
return String.valueOf(0);
|
||||
case MINUTES:
|
||||
return String.valueOf(leftOver);
|
||||
case SECONDS:
|
||||
return String.valueOf(secondsLeft);
|
||||
return String.valueOf(seconds / 86400);
|
||||
}
|
||||
|
||||
return String.valueOf(seconds);
|
||||
|
||||
} else {
|
||||
int hours = leftOver / 60;
|
||||
int hoursInMins = 60 * hours;
|
||||
int minsLeft = leftOver - hoursInMins;
|
||||
|
||||
switch (type) {
|
||||
case DAYS:
|
||||
return String.valueOf(days);
|
||||
case HOURS:
|
||||
return String.valueOf(hours);
|
||||
case MINUTES:
|
||||
return String.valueOf(minsLeft);
|
||||
case SECONDS:
|
||||
return String.valueOf(secondsLeft);
|
||||
}
|
||||
|
||||
return String.valueOf(seconds);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getTime(int seconds) {
|
||||
return getTime(Duration.ofSeconds(seconds));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -126,46 +57,40 @@ public class TimeUtil {
|
||||
* @return formatted time
|
||||
*/
|
||||
public static String getTime(final Duration duration) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
return getTime(duration.getSeconds());
|
||||
}
|
||||
|
||||
public static String getTime(final int seconds) {
|
||||
return getTime((long) seconds);
|
||||
}
|
||||
|
||||
public static String getTime(long seconds) {
|
||||
final StringJoiner joiner = new StringJoiner(" ");
|
||||
|
||||
long seconds = duration.getSeconds();
|
||||
long minutes = seconds / 60;
|
||||
long hours = minutes / 60;
|
||||
long days = hours / 24;
|
||||
final long days = hours / 24;
|
||||
|
||||
seconds %= 60;
|
||||
minutes %= 60;
|
||||
hours %= 60;
|
||||
days %= 24;
|
||||
hours %= 24;
|
||||
|
||||
if (seconds > 0) {
|
||||
builder.insert(0, seconds + "s");
|
||||
}
|
||||
|
||||
if (minutes > 0) {
|
||||
if (builder.length() > 0) {
|
||||
builder.insert(0, ' ');
|
||||
}
|
||||
|
||||
builder.insert(0, minutes + "m");
|
||||
if (days > 0) {
|
||||
joiner.add(days + "d");
|
||||
}
|
||||
|
||||
if (hours > 0) {
|
||||
if (builder.length() > 0) {
|
||||
builder.insert(0, ' ');
|
||||
joiner.add(hours + "h");
|
||||
}
|
||||
|
||||
builder.insert(0, hours + "h");
|
||||
if (minutes > 0) {
|
||||
joiner.add(minutes + "m");
|
||||
}
|
||||
|
||||
if (days > 0) {
|
||||
if (builder.length() > 0) {
|
||||
builder.insert(0, ' ');
|
||||
if (seconds > 0) {
|
||||
joiner.add(seconds + "s");
|
||||
}
|
||||
|
||||
builder.insert(0, days + "d");
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
return joiner.toString();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user