Merge pull request #118 from itHotL/time-unit-fixes

Made the time formatting a bit more simple and dynamic
This commit is contained in:
Elise 2022-08-17 12:49:27 +02:00 committed by GitHub
commit 13a557e8e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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++;
}
return output.append(format.format(days))
.append("d").toString();
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());
}
output.append(format.format(days))
.append("d");
}
if (isInRange(max, min, 3600) && leftover >= 3600) {
if (output.toString().contains("d")) {
//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());
}
//We did not have enough leftover to fill a unit
else{
if(output.toString().length() != 0){
output.append(" 0").append(currUnit.getShortLabel());
}
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++;
}
return output.append(format.format(hours))
.append("h").toString();
output.append(amount).append(currUnit.getShortLabel());
//Check if we need to break
if(currUnit == smallestUnit || leftoverSeconds == 0){
break;
}
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;
}
}