Made the time formatting a bit more simple and dynamic

This commit is contained in:
Robin 2022-08-15 23:22:17 +02:00
parent 3a9eb86c1e
commit f238221511
2 changed files with 53 additions and 58 deletions

View File

@ -111,6 +111,18 @@ public enum Unit {
};
}
/** Converts the current Unit into a short label (and returns a '?' if the current Unit is not of Type TIME)*/
public char getShortLabel(){
return switch (this) {
case DAY -> 'd';
case HOUR -> 'h';
case MINUTE -> 'm';
case SECOND -> 's';
case TICK -> 't';
default -> '?';
};
}
/** Returns the Unit corresponding to the given String. This String does NOT need to
match exactly (it can be "day" or "days", for example), and is case-insensitive.
@param unitName an approximation of the name belonging to the desired Unit, case-insensitive */

View File

@ -54,76 +54,59 @@ public final class NumberFormatter {
}
/** The unit of time-based statistics is ticks by default.*/
public String formatTimeNumber(long number, Unit bigUnit, Unit smallUnit) { //5 statistics
if (number == 0) {
public String formatTimeNumber(long number, Unit biggestUnit, Unit smallestUnit) { //5 statistics
if (number <= 0) {
return "-";
}
if (bigUnit == Unit.TICK && smallUnit == Unit.TICK || bigUnit == Unit.NUMBER || smallUnit == Unit.NUMBER) {
if (biggestUnit == Unit.TICK && smallestUnit == Unit.TICK || biggestUnit == Unit.NUMBER || smallestUnit == Unit.NUMBER) {
return format.format(number);
}
Unit currUnit = biggestUnit;
int leftoverSeconds = (int) Math.round(number / 20.0);
StringBuilder output = new StringBuilder();
double max = bigUnit.getSeconds();
double min = smallUnit.getSeconds();
double leftover = number / 20.0;
if (isInRange(max, min, 86400) && leftover >= 86400) {
double days = Math.floor(leftover / 86400);
leftover = leftover % (86400);
if (smallUnit == Unit.DAY) {
if (leftover >= 43200) {
days++;
while(currUnit != null){
//Define amount of units
int amount = 0;
//Current unit is equal to smallest unit, in this case round the remainder
if(currUnit == smallestUnit){
amount = (int) Math.round(leftoverSeconds / currUnit.getSeconds());
}
return output.append(format.format(days))
.append("d").toString();
//Leftover amount of seconds is greater than current unit, we can extract x units
else if(leftoverSeconds >= currUnit.getSeconds()){
amount = (int) Math.floor(leftoverSeconds / currUnit.getSeconds());
}
output.append(format.format(days))
.append("d");
//We did not have enough leftover to fill a unit
else{
if(output.toString().length() != 0){
output.append(" 0").append(currUnit.getShortLabel());
}
if (isInRange(max, min, 3600) && leftover >= 3600) {
if (output.toString().contains("d")) {
currUnit = currUnit.getSmallerUnit(1);
continue;
}
//Calculate new leftover
leftoverSeconds = leftoverSeconds - (int)(amount * currUnit.getSeconds());
//Append new values
if(output.toString().length() != 0){
output.append(" ");
}
double hours = Math.floor(leftover / 60 / 60);
leftover = leftover % (60 * 60);
if (smallUnit == Unit.HOUR) {
if (leftover >= 1800) {
hours++;
output.append(amount).append(currUnit.getShortLabel());
//Check if we need to break
if(currUnit == smallestUnit || leftoverSeconds == 0){
break;
}
return output.append(format.format(hours))
.append("h").toString();
}
output.append(format.format(hours))
.append("h");
}
if (isInRange(max, min, 60) && leftover >= 60) {
if (output.toString().contains("h")) {
output.append(" ");
}
double minutes = Math.floor(leftover / 60);
leftover = leftover % 60;
if (smallUnit == Unit.MINUTE) {
if (leftover >= 30) {
minutes++;
}
return output.append(format.format(minutes))
.append("m").toString();
}
output.append(format.format(minutes))
.append("m");
}
if (isInRange(max, min, 1) && leftover > 0) {
if (output.toString().contains("m")) {
output.append(" ");
}
double seconds = Math.ceil(leftover);
output.append(format.format(seconds))
.append("s");
//Lower current unit by 1
currUnit = currUnit.getSmallerUnit(1);
}
return output.toString();
}
private boolean isInRange(double bigUnit, double smallUnit, double unitToEvaluate) {
return bigUnit >= unitToEvaluate && unitToEvaluate >= smallUnit;
}
}