play.libs.Time Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
RePlay is a fork of the Play1 framework, created by Codeborne.
package play.libs;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Time utils
*
* Provides a parser for time expression.
*
* Time expressions provide the ability to specify complex time combinations
* such as "2d", "1w2d3h10s" or "2d4h10s".
*
*
*/
public class Time {
private static final Pattern p = Pattern.compile("(([0-9]+?)((d|h|mi|min|mn|s)))+?");
private static final Integer MINUTE = 60;
private static final Integer HOUR = 60 * MINUTE;
private static final Integer DAY = 24 * HOUR;
/**
* Parse a duration
*
* @param duration
* 3h, 2mn, 7s or combination 2d4h10s, 1w2d3h10s
* @return The number of seconds
*/
public static int parseDuration(String duration) {
if (duration == null) {
throw new IllegalArgumentException("duration cannot be null");
}
Matcher matcher = p.matcher(duration);
int seconds = 0;
if (!matcher.matches()) {
throw new IllegalArgumentException(String.format("Invalid duration pattern: \"%s\"", duration));
}
matcher.reset();
while (matcher.find()) {
switch (matcher.group(3)) {
case "d":
seconds += Integer.parseInt(matcher.group(2)) * DAY;
break;
case "h":
seconds += Integer.parseInt(matcher.group(2)) * HOUR;
break;
case "mi":
case "min":
case "mn":
seconds += Integer.parseInt(matcher.group(2)) * MINUTE;
break;
default:
seconds += Integer.parseInt(matcher.group(2));
break;
}
}
return seconds;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy