Created
January 19, 2021 19:19
-
-
Save ChocolatMilka/e7f7d51ae2391fdaf83c08a51ce65bd6 to your computer and use it in GitHub Desktop.
Permet de manipuler des durées, les convertir en expression littéral ou en seconde.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package fr.wizmc.api.utils.textual; | |
| import java.util.StringJoiner; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.regex.Matcher; | |
| import java.util.regex.Pattern; | |
| /** | |
| * Permet de manipuler les durée au niveau littéral | |
| * | |
| * @author 360matt | |
| * | |
| */ | |
| public class TimeUtils { | |
| /** | |
| * Permet d'avoir le nombre de seconde à partir d'un texte | |
| * @param text String ayant le formattage chiffre suivi d'une séquence de lettre, pouvant être répétés | |
| * @return le nombre de secondes représentant la durée parsée | |
| * | |
| * @author 360matt | |
| */ | |
| public static long textToSeconds (final String text) { | |
| final Pattern pattern = Pattern.compile("([0-9]+)([A-z]+)", Pattern.MULTILINE); | |
| final Matcher matcher = pattern.matcher(text); | |
| long res = 0; | |
| while (matcher.find()) { | |
| final int number = Integer.parseInt(matcher.group(1)); | |
| final String multiplier = matcher.group(2); | |
| if (multiplier.startsWith("s")) res += number; | |
| else if (multiplier.startsWith("min")) res += number*60L; | |
| else if (multiplier.startsWith("h")) res += number*3600L; | |
| else if (multiplier.startsWith("d")) res += number*86400L; | |
| else if (multiplier.startsWith("j")) res += number*86400L; | |
| else if (multiplier.startsWith("w")) res += number*604800L; | |
| else if (multiplier.startsWith("sem")) res += number*604800L; | |
| else if (multiplier.startsWith("mo")) res += number*2592000L; | |
| else if (multiplier.startsWith("y")) res += number*31536000L; | |
| else if (multiplier.startsWith("an")) res += number*31536000L; | |
| } | |
| return res; | |
| } | |
| /** | |
| * | |
| * Permet d'obtenir un texte très français représentant une durée à partir du nombre de seconde. | |
| * Supportant le principe de singularité / pluriel | |
| * | |
| * @param seconds le nombre de seconde | |
| * @return le texte représentant la durée en français | |
| */ | |
| public static String secondsToText (int seconds) { | |
| final String[] indices = new String[]{"année", "mois", "jour", "heure", "minute", "seconde"}; | |
| final int[] multiples = new int[]{31536000, 2592000, 86400, 3600, 60, 1}; | |
| final StringJoiner sj = new StringJoiner(", "); | |
| for (int i = 0; i < indices.length; i++) { | |
| // Amount : correspond à la quantité d'un unité de durée | |
| // multiples[i] : correspond au nombre de seconde qu'il faut pour 1 unité de temps | |
| int amount = Math.round(seconds / multiples[i]); | |
| seconds -= amount * multiples[i]; | |
| if (amount > 0) | |
| sj.add(amount + " " + indices[i] + ((amount > 1 && i != 1) ? "s" : "")); | |
| // /\ /\ | |
| // | | | |
| // Cette partie ajoute un S à l'unité si pluriel, sauf s'il s'agit du mois | |
| } | |
| return sj.toString(); | |
| } | |
| /** | |
| * Permet de mapper les différents unité de durée à partir d'une durée en milliseconde | |
| * | |
| * @author Hyrezzz, 360matt (reformat) | |
| */ | |
| public static class TimeMapped { | |
| public final int seconds; | |
| public final int minutes; | |
| public final int hours; | |
| public final int days; | |
| public TimeMapped (final long milliseconds) { | |
| this.seconds = (int)(milliseconds / 1000L) % 60; | |
| this.minutes = (int)(milliseconds / 60000L) % 60; | |
| this.hours = (int)(milliseconds / 3600000L) % 24; | |
| this.days = (int)(milliseconds / 86400000L); | |
| } | |
| } | |
| /** | |
| * Génère un TimeMapped utilisé pour répartir les unités de durée et leur valeurs | |
| * | |
| * @param milliseconds la durée exprimée en milliseconde | |
| * @return un objet TimeMapped | |
| */ | |
| public static TimeMapped getTimeMapped (final long milliseconds) { | |
| return new TimeMapped(milliseconds); | |
| } | |
| // From: MILLISECONDS | |
| public static double msToSeconds (final long milliseconds) { return TimeUnit.MILLISECONDS.toSeconds(milliseconds); } | |
| public static double msToMinutes (final long milliseconds) { return TimeUnit.MILLISECONDS.toMinutes(milliseconds); } | |
| public static double msToHours (final long milliseconds) { return TimeUnit.MILLISECONDS.toHours(milliseconds); } | |
| public static double msToDay (final long milliseconds) { return TimeUnit.MILLISECONDS.toDays(milliseconds); } | |
| // From: SECONDS | |
| public static double secToMilliseconds (final long seconds) { return TimeUnit.SECONDS.toMillis(seconds); } | |
| public static double secToMinutes (final long seconds) { return TimeUnit.MILLISECONDS.toMinutes(seconds); } | |
| public static double secToHours (final long seconds) { return TimeUnit.MILLISECONDS.toHours(seconds); } | |
| public static double secToDay (final long seconds) { return TimeUnit.MILLISECONDS.toDays(seconds); } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment