mirror of
https://github.com/PlaceholderAPI/PlaceholderAPI
synced 2025-02-05 15:55:28 +01:00
Refractor of TimeUtil
This commit is contained in:
parent
0da55ba037
commit
e97cbbc4f8
@ -40,7 +40,7 @@ public final class Format {
|
||||
|
||||
@NotNull
|
||||
public static Optional<List<String>> tablify(@NotNull final Align align,
|
||||
@NotNull final List<List<String>> rows) {
|
||||
@NotNull final List<List<String>> rows) {
|
||||
return findSpacing(rows)
|
||||
.map(spacing -> buildFormat(align, spacing))
|
||||
.map(format -> rows.stream()
|
||||
|
@ -1,28 +1,49 @@
|
||||
/*
|
||||
* This file is part of PlaceholderAPI
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (c) 2015 - 2024 PlaceholderAPI Team
|
||||
*
|
||||
* PlaceholderAPI free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PlaceholderAPI is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package me.clip.placeholderapi.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.StringJoiner;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
|
||||
public enum TimeFormat {
|
||||
DAYS,
|
||||
HOURS,
|
||||
MINUTES,
|
||||
SECONDS
|
||||
|
||||
DAYS(seconds -> seconds / 86400),
|
||||
HOURS(seconds -> (seconds / 3600) % 24),
|
||||
MINUTES(seconds -> (seconds / 60) % 60),
|
||||
SECONDS(seconds -> seconds % 60);
|
||||
|
||||
private final IntUnaryOperator convert;
|
||||
|
||||
TimeFormat(IntUnaryOperator convert) {
|
||||
this.convert = convert;
|
||||
}
|
||||
|
||||
public int convert(int seconds) {
|
||||
return convert.applyAsInt(seconds);
|
||||
}
|
||||
|
||||
public String formatTime(Duration duration) {
|
||||
return formatTime((int) duration.getSeconds());
|
||||
}
|
||||
|
||||
public String formatTime(int seconds) {
|
||||
final StringJoiner joiner = new StringJoiner(" ");
|
||||
|
||||
int remainingSeconds = seconds;
|
||||
int days = DAYS.convert(remainingSeconds);
|
||||
remainingSeconds -= days * 86400;
|
||||
|
||||
int hours = HOURS.convert(remainingSeconds);
|
||||
remainingSeconds -= hours * 3600;
|
||||
|
||||
int minutes = MINUTES.convert(remainingSeconds);
|
||||
remainingSeconds -= minutes * 60;
|
||||
|
||||
if (days > 0) joiner.add(days + "d");
|
||||
if (hours > 0) joiner.add(hours + "h");
|
||||
if (minutes > 0) joiner.add(minutes + "m");
|
||||
if (remainingSeconds > 0) joiner.add(remainingSeconds + "s");
|
||||
|
||||
return joiner.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,85 +1,47 @@
|
||||
/*
|
||||
* This file is part of PlaceholderAPI
|
||||
*
|
||||
* PlaceholderAPI
|
||||
* Copyright (c) 2015 - 2024 PlaceholderAPI Team
|
||||
*
|
||||
* PlaceholderAPI free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PlaceholderAPI is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package me.clip.placeholderapi.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class TimeUtil {
|
||||
|
||||
/**
|
||||
* Retrieves the remaining time in the specified format.
|
||||
*
|
||||
* @param seconds the total number of seconds to convert
|
||||
* @param type the desired time format (DAYS, HOURS, MINUTES, SECONDS)
|
||||
* @return a string representing the remaining value in the specified format
|
||||
*/
|
||||
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) {
|
||||
default:
|
||||
return String.valueOf(seconds);
|
||||
|
||||
case SECONDS:
|
||||
return String.valueOf(seconds % 60);
|
||||
|
||||
case MINUTES:
|
||||
return String.valueOf((seconds / 60) % 60);
|
||||
|
||||
case HOURS:
|
||||
return String.valueOf((seconds / 3600) % 24);
|
||||
|
||||
case DAYS:
|
||||
return String.valueOf(seconds / 86400);
|
||||
}
|
||||
return String.valueOf(type.convert(seconds));
|
||||
}
|
||||
|
||||
/**
|
||||
* Format the given value with s, m, h and d (seconds, minutes, hours and days)
|
||||
* Formats the given duration into a human-readable string.
|
||||
*
|
||||
* @param duration {@link Duration} (eg, Duration.of(20, {@link ChronoUnit#SECONDS}) for 20
|
||||
* seconds)
|
||||
* @return formatted time
|
||||
* @param duration the {@link Duration} to be formatted (e.g., Duration.of(20, ChronoUnit.SECONDS))
|
||||
* @return a formatted string representing the duration (e.g., "1d 3h 20m 15s")
|
||||
*/
|
||||
public static String getTime(final Duration duration) {
|
||||
return getTime(duration.getSeconds());
|
||||
return TimeFormat.DAYS.formatTime(duration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given number of seconds into a human-readable string.
|
||||
*
|
||||
* @param seconds the total number of seconds to format
|
||||
* @return a formatted string representing the time (e.g., "1d 3h 20m 15s")
|
||||
*/
|
||||
public static String getTime(final int seconds) {
|
||||
return getTime((long) seconds);
|
||||
return TimeFormat.DAYS.formatTime(seconds);
|
||||
}
|
||||
|
||||
public static String getTime(long seconds) {
|
||||
final StringJoiner joiner = new StringJoiner(" ");
|
||||
|
||||
long minutes = seconds / 60;
|
||||
long hours = minutes / 60;
|
||||
final long days = hours / 24;
|
||||
|
||||
seconds %= 60;
|
||||
minutes %= 60;
|
||||
hours %= 24;
|
||||
|
||||
if (days > 0) joiner.add(days + "d");
|
||||
if (hours > 0) joiner.add(hours + "h");
|
||||
if (minutes > 0) joiner.add(minutes + "m");
|
||||
if (seconds > 0) joiner.add(seconds + "s");
|
||||
|
||||
return joiner.toString();
|
||||
/**
|
||||
* Formats the given number of seconds into a human-readable string.
|
||||
*
|
||||
* @param seconds the total number of seconds to format
|
||||
* @return a formatted string representing the time (e.g., "1d 3h 20m 15s")
|
||||
*/
|
||||
public static String getTime(final long seconds) {
|
||||
return TimeFormat.DAYS.formatTime((int) seconds);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user