Skip to content

Commit

Permalink
Support 00:00:00.00 format
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Jan 6, 2023
1 parent b89e56a commit f3f2709
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.Nullable;
import us.ajg0702.leaderboards.LeaderboardPlugin;
import us.ajg0702.leaderboards.formatting.formats.ColonTime;
import us.ajg0702.leaderboards.formatting.formats.Default;
import us.ajg0702.leaderboards.formatting.formats.Time;

Expand All @@ -14,6 +15,7 @@ public class PlaceholderFormatter {
private final Format defaultFormat = new Default();
private final List<Format> formats = Arrays.asList(
new Time(),
new ColonTime(),

defaultFormat
);
Expand Down
@@ -0,0 +1,57 @@
package us.ajg0702.leaderboards.formatting.formats;

import us.ajg0702.leaderboards.formatting.Format;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ColonTime extends Format {

private final Pattern pattern = Pattern.compile("([0-9]*)?:([0-9]*):([0-9]*)(:|.)?([0-9]*)?");
@Override
public boolean matches(String output, String placeholder) {
if(output == null) return false;
return pattern.matcher(output).matches();
}

@Override
public double toDouble(String input) throws NumberFormatException {
Matcher matcher = pattern.matcher(input);

int hours = Integer.parseInt(matcher.group(1));
int minutes = Integer.parseInt(matcher.group(2));
int seconds = Integer.parseInt(matcher.group(3));

String secondSeperator = matcher.group(4);
int miliSeconds = matcher.group(5) != null ? Integer.parseInt(matcher.group(5)) : -1;

double result = 0;

result += seconds;
result += minutes * 60;
result += hours * 60 * 60;

if(secondSeperator != null && miliSeconds != -1) {
if(secondSeperator.equals(":")) {
result += miliSeconds / 1000d;
} else if(secondSeperator.equals(".")) {
result += Integer.parseInt("0." + miliSeconds);
}
}

return result;

}

@Override
public String toFormat(double input) {
int hours = (int) (input / (60 * 60));
int minutes = (int) ((input % (60 * 60)) / 60);
double seconds = input % 60d;

return
hours == 0 ? "00" : hours +
minutes == 0 ? "00" : minutes +
seconds == 0 ? "00" : seconds + "";
}
}
@@ -0,0 +1,45 @@
package us.ajg0702.leaderboards.formatting.formats;

import junit.framework.TestCase;

import java.util.Arrays;
import java.util.List;

public class ColonTimeTest extends TestCase {

public void testMatches() throws Exception {
List<String> shouldMatch = Arrays.asList(
"00:00:00.00",
"00:00:00:00",
"1:2:3:4",
"1:2:3.4",
"11:22:33:44",
"11:22:33.44",
"12:3:15.735"
);
List<String> shouldNotMatch = Arrays.asList(
"hello there",
"15e",
"12n 15b",
"[60]",
"123456",
"13h 21m"
);

ColonTime colonTimeFormat = new ColonTime();

for (String match : shouldMatch) {
boolean res = colonTimeFormat.matches(match, "");
if(!res) {
throw new Exception(match + " did not match when it should have!");
}
}

for (String notMatch : shouldNotMatch) {
if(colonTimeFormat.matches(notMatch, "")) {
throw new Exception(notMatch + " did match when it should not have!");
}
}
}

}

0 comments on commit f3f2709

Please sign in to comment.