Improved TimeUtil + fix BukkitChatColorUtil
This commit is contained in:
parent
c8b626f655
commit
ea1cf90119
@ -33,11 +33,18 @@ public class TimeUtil {
|
|||||||
|
|
||||||
private static DateTimeFormatter HMSFormatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault());
|
private static DateTimeFormatter HMSFormatter = DateTimeFormatter.ofPattern("HH:mm:ss", Locale.getDefault());
|
||||||
private static DateTimeFormatter HMFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault());
|
private static DateTimeFormatter HMFormatter = DateTimeFormatter.ofPattern("HH:mm", Locale.getDefault());
|
||||||
|
private static DateTimeFormatter HFormatter = DateTimeFormatter.ofPattern("H'h'", Locale.getDefault());
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public static String relativeDateFr(long displayTime, boolean showSeconds, boolean compactWords) {
|
public static String relativeDateFr(long displayTime, boolean showSeconds, boolean compactWords) {
|
||||||
|
return relativeDateFr(displayTime,
|
||||||
|
showSeconds ? RelativePrecision.MINUTES : RelativePrecision.SECONDS,
|
||||||
|
showSeconds ? DisplayPrecision.MINUTES : DisplayPrecision.SECONDS,
|
||||||
|
compactWords);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String relativeDateFr(long displayTime, RelativePrecision relPrecision, DisplayPrecision dispPrecision, boolean compactWords) {
|
||||||
long currentTime = System.currentTimeMillis();
|
long currentTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
|
||||||
@ -50,76 +57,121 @@ public class TimeUtil {
|
|||||||
|
|
||||||
if (timeDiffSec < -1) {
|
if (timeDiffSec < -1) {
|
||||||
// in the future
|
// in the future
|
||||||
if (timeDiffSec > -60) {
|
if (relPrecision == RelativePrecision.SECONDS) {
|
||||||
if (showSeconds)
|
if (timeDiffSec > -60)
|
||||||
return "dans " + (-timeDiffSec) + " secondes";
|
return "dans " + (-timeDiffSec) + (compactWords ? "s" : " secondes");
|
||||||
else
|
|
||||||
return "dans moins d’une minute";
|
|
||||||
}
|
}
|
||||||
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.MINUTES)) {
|
||||||
|
if (timeDiffSec > -60)
|
||||||
|
return compactWords ? "dans moins d’1min" : "dans moins d’une minute";
|
||||||
if (timeDiffSec > -60*2) // dans 2 min
|
if (timeDiffSec > -60*2) // dans 2 min
|
||||||
return "dans ̈" + (int)Math.floor((-timeDiffSec)/60) + " minute";
|
return compactWords ? "dans 1min" : "dans une minute";
|
||||||
if (timeDiffSec > -3600) // dans 1h
|
if (timeDiffSec > -3600) // dans moins d’1h
|
||||||
return "dans " + (int)Math.floor((-timeDiffSec)/60) + " minutes";
|
return "dans " + (int)Math.floor((-timeDiffSec)/60) + (compactWords ? "min" : " minutes");
|
||||||
if (timeDiffSec > -3600*2) // dans 2h
|
}
|
||||||
return "dans " + (int)Math.floor((-timeDiffSec)/(3600)) + " heure";
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.HOURS)) {
|
||||||
if (timeDiffSec > -3600*12) // dans 12h
|
if (timeDiffSec > -3600) // dans moins d’1h
|
||||||
return "dans " + (int)Math.floor((-timeDiffSec)/(3600)) + " heures";
|
return compactWords ? "dans moins d’1h" : "dans moins d’une heure";
|
||||||
|
if (timeDiffSec > -3600*2) // dans moins de 2h
|
||||||
|
return compactWords ? "dans 1h" : "dans une heure";
|
||||||
|
if (timeDiffSec > -3600*12) // dans moins de 12h
|
||||||
|
return "dans " + (int)Math.floor((-timeDiffSec)/3600) + (compactWords ? "h" : " heures");
|
||||||
|
}
|
||||||
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.DAYS)) {
|
||||||
LocalDateTime nextMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0).plusDays(1);
|
LocalDateTime nextMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0).plusDays(1);
|
||||||
if (displayDateTime.isBefore(nextMidnight)) // aujourd'hui
|
if (displayDateTime.isBefore(nextMidnight)) // aujourd'hui
|
||||||
return "aujourd’hui à " + (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
return "aujourd’hui à " + dayTimeFr(displayTime, dispPrecision);
|
||||||
if (displayDateTime.isBefore(nextMidnight.plusDays(1))) // demain
|
if (displayDateTime.isBefore(nextMidnight.plusDays(1))) // demain
|
||||||
return "demain à " + (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
return "demain à " + dayTimeFr(displayTime, dispPrecision);
|
||||||
if (displayDateTime.isBefore(nextMidnight.plusDays(5))) // dans moins d'1 semaine
|
if (displayDateTime.isBefore(nextMidnight.plusDays(5))) // dans moins d'1 semaine
|
||||||
return (compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " "
|
return (compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " "
|
||||||
+ dayOfMonthFormatter.format(displayDateTime) + " à "
|
+ dayOfMonthFormatter.format(displayDateTime) + " à "
|
||||||
+ (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
+ dayTimeFr(displayTime, dispPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
return fullDateFr(displayTime, showSeconds, true, compactWords);
|
return fullDateFr(displayTime, dispPrecision, true, compactWords);
|
||||||
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// present and past
|
// present and past
|
||||||
if (timeDiffSec <= 1)
|
if (timeDiffSec <= 1)
|
||||||
return "maintenant";
|
return "maintenant";
|
||||||
if (timeDiffSec < 60) { // ya moins d'1 min
|
|
||||||
if (showSeconds)
|
if (relPrecision == RelativePrecision.SECONDS) {
|
||||||
return "il y a " + timeDiffSec + " secondes";
|
if (timeDiffSec < 60) // ya moins d'1 min
|
||||||
else
|
return "il y a " + timeDiffSec + (compactWords ? "s" : " secondes");
|
||||||
return "il y a moins d’une minute";
|
|
||||||
}
|
}
|
||||||
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.MINUTES)) {
|
||||||
|
if (timeDiffSec < 60) // ya moins d'1 min
|
||||||
|
return compactWords ? "il y a moins d’1min" : "il y a moins d’une minute";
|
||||||
if (timeDiffSec < 60*2) // ya moins de 2 min
|
if (timeDiffSec < 60*2) // ya moins de 2 min
|
||||||
return "il y a " + (int)Math.floor((timeDiffSec)/60) + " minute";
|
return compactWords ? "il y a 1min" : "il y a une minute";
|
||||||
if (timeDiffSec < 3600) // ya moins d'1h
|
if (timeDiffSec < 3600) // ya moins d'1h
|
||||||
return "il y a " + (int)Math.floor((timeDiffSec)/60) + " minutes";
|
return "il y a " + (int)Math.floor(timeDiffSec/60) + (compactWords ? "min" : " minutes");
|
||||||
|
}
|
||||||
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.HOURS)) {
|
||||||
|
if (timeDiffSec < 3600) // ya moins d'1h
|
||||||
|
return "il y a moins d’une heure";
|
||||||
if (timeDiffSec < 3600*2) // ya moins de 2h
|
if (timeDiffSec < 3600*2) // ya moins de 2h
|
||||||
return "il y a " + (int)Math.floor((timeDiffSec)/(3600)) + " heure";
|
return "il y a une heure";
|
||||||
if (timeDiffSec < 3600*12) // ya moins de 12h
|
if (timeDiffSec < 3600*12) // ya moins de 12h
|
||||||
return "il y a " + (int)Math.floor((timeDiffSec)/(3600)) + " heures";
|
return "il y a " + (int)Math.floor(timeDiffSec/3600) + " heures";
|
||||||
|
}
|
||||||
|
if (relPrecision.morePreciseOrEqTo(RelativePrecision.DAYS)) {
|
||||||
LocalDateTime lastMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0);
|
LocalDateTime lastMidnight = LocalDateTime.of(currentDateTime.getYear(), currentDateTime.getMonth(), currentDateTime.getDayOfMonth(), 0, 0);
|
||||||
|
|
||||||
if (!displayDateTime.isBefore(lastMidnight)) // aujourd'hui
|
if (!displayDateTime.isBefore(lastMidnight)) // aujourd'hui
|
||||||
return "aujourd’hui à " + (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
return "aujourd’hui à " + dayTimeFr(displayTime, dispPrecision);
|
||||||
if (!displayDateTime.isBefore(lastMidnight.minusDays(1))) // hier
|
if (!displayDateTime.isBefore(lastMidnight.minusDays(1))) // hier
|
||||||
return "hier à " + (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
return "hier à " + dayTimeFr(displayTime, dispPrecision);
|
||||||
if (!displayDateTime.isBefore(lastMidnight.minusDays(6))) // ya moins d'1 semaine
|
if (!displayDateTime.isBefore(lastMidnight.minusDays(6))) // ya moins d'1 semaine
|
||||||
return (compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " dernier à "
|
return (compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " dernier à "
|
||||||
+ (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
+ dayTimeFr(displayTime, dispPrecision);
|
||||||
|
}
|
||||||
|
|
||||||
return fullDateFr(displayTime, showSeconds, true, compactWords);
|
return fullDateFr(displayTime, dispPrecision, true, compactWords);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public enum RelativePrecision {
|
||||||
|
NONE, DAYS, HOURS, MINUTES, SECONDS;
|
||||||
|
|
||||||
|
public boolean morePreciseThan(RelativePrecision o) { return ordinal() > o.ordinal(); }
|
||||||
|
public boolean lessPreciseThan(RelativePrecision o) { return ordinal() < o.ordinal(); }
|
||||||
|
public boolean morePreciseOrEqTo(RelativePrecision o) { return ordinal() >= o.ordinal(); }
|
||||||
|
public boolean lessPreciseOrEqTo(RelativePrecision o) { return ordinal() <= o.ordinal(); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum DisplayPrecision {
|
||||||
|
DAYS, HOURS, MINUTES, SECONDS
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static String fullDateFr(long displayTime, boolean showSeconds, boolean showWeekday, boolean compactWords) {
|
public static String fullDateFr(long displayTime, boolean showSeconds, boolean showWeekday, boolean compactWords) {
|
||||||
|
return fullDateFr(displayTime, showSeconds ? DisplayPrecision.SECONDS : DisplayPrecision.MINUTES, showWeekday, compactWords);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String fullDateFr(long displayTime, DisplayPrecision precision, boolean showWeekday, boolean compactWords) {
|
||||||
LocalDateTime displayDateTime = toLocalDateTime(displayTime);
|
LocalDateTime displayDateTime = toLocalDateTime(displayTime);
|
||||||
return (showWeekday ? ((compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " ") : "")
|
String ret = (showWeekday ? ((compactWords ? cmpDayOfWeekFormatter : dayOfWeekFormatter).format(displayDateTime) + " ") : "")
|
||||||
+ dayOfMonthFormatter.format(displayDateTime) + " "
|
+ dayOfMonthFormatter.format(displayDateTime) + " "
|
||||||
+ (compactWords ? cmpMonthFormatter : monthFormatter).format(displayDateTime) + " "
|
+ (compactWords ? cmpMonthFormatter : monthFormatter).format(displayDateTime) + " "
|
||||||
+ yearFormatter.format(displayDateTime) + " à "
|
+ yearFormatter.format(displayDateTime);
|
||||||
+ (showSeconds ? HMSFormatter : HMFormatter).format(displayDateTime);
|
if (precision == DisplayPrecision.DAYS)
|
||||||
|
return ret;
|
||||||
|
return ret + " à " + dayTimeFr(displayTime, precision);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String dayTimeFr(long displayTime, DisplayPrecision precision) {
|
||||||
|
DateTimeFormatter tFormatter = switch(precision) {
|
||||||
|
case HOURS -> HFormatter;
|
||||||
|
case MINUTES -> HMFormatter;
|
||||||
|
case SECONDS -> HMSFormatter;
|
||||||
|
default -> throw new IllegalArgumentException("precision");
|
||||||
|
};
|
||||||
|
return tFormatter.format(toLocalDateTime(displayTime));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,11 +72,11 @@ public class BukkitChatColorUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static org.bukkit.ChatColor toBukkit(NamedTextColor color) {
|
public static org.bukkit.ChatColor toBukkit(NamedTextColor color) {
|
||||||
return org.bukkit.ChatColor.valueOf(color.toString());
|
return org.bukkit.ChatColor.valueOf(color.toString().toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static NamedTextColor toAdventure(org.bukkit.ChatColor color) {
|
public static NamedTextColor toAdventure(org.bukkit.ChatColor color) {
|
||||||
return NamedTextColor.NAMES.value(color.name());
|
return NamedTextColor.NAMES.value(color.name().toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user