ShowControl/src/main/java/nl/sbdeveloper/showapi/utils/TimeUtil.java

76 lines
2.4 KiB
Java
Raw Normal View History

2020-11-12 22:23:49 +01:00
package nl.sbdeveloper.showapi.utils;
import java.time.LocalTime;
import java.time.format.DateTimeParseException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TimeUtil {
private static final int s = 1000;
private static final int m = s * 60;
private static final int h = m * 60;
private static final int d = h * 24;
private static final int w = d * 7;
private static final int y = (int)(d * 365.25);
public static String showTime(int seconds) {
2021-01-22 20:46:24 +01:00
LocalTime timeOfDay = LocalTime.ofSecondOfDay(seconds / 20);
2020-11-12 22:23:49 +01:00
return timeOfDay.toString();
}
2021-01-22 20:46:24 +01:00
public static int parseTicks(String str) {
2020-11-12 22:23:49 +01:00
try {
LocalTime localTime = LocalTime.parse(str);
return localTime.toSecondOfDay();
} catch (DateTimeParseException ex) {
2021-01-22 20:46:24 +01:00
Pattern pattern = Pattern.compile("^(-?(?:\\d+)?\\.?\\d+) *(ticks?|tick?|t|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$");
2020-11-12 22:23:49 +01:00
Matcher matcher = pattern.matcher(str);
if (!matcher.find()) return 0;
float n = Float.parseFloat(matcher.group(1));
switch (matcher.group(2).toLowerCase()) {
case "years":
case "year":
case "yrs":
case "yr":
case "y":
2021-01-22 20:46:24 +01:00
return (int)(n * y) / 20000;
2020-11-12 22:23:49 +01:00
case "weeks":
case "week":
case "w":
2021-01-22 20:46:24 +01:00
return (int)(n * w) / 20000;
2020-11-12 22:23:49 +01:00
case "days":
case "day":
case "d":
2021-01-22 20:46:24 +01:00
return (int)(n * d) / 20000;
2020-11-12 22:23:49 +01:00
case "hours":
case "hour":
case "hrs":
case "hr":
case "h":
2021-01-22 20:46:24 +01:00
return (int)(n * h) / 20000;
2020-11-12 22:23:49 +01:00
case "minutes":
case "minute":
case "mins":
case "min":
case "m":
2021-01-22 20:46:24 +01:00
return (int)(n * m) / 20000;
2020-11-12 22:23:49 +01:00
case "seconds":
case "second":
case "secs":
case "sec":
case "s":
2021-01-22 20:46:24 +01:00
return (int)(n * s) / 20000;
case "ticks":
case "tick":
case "ts":
case "t":
return (int) n / 20000;
2020-11-12 22:23:49 +01:00
default:
return 0;
}
}
}
}