Skip to content

Commit

Permalink
Merge pull request #118 from itHotL/time-unit-fixes
Browse files Browse the repository at this point in the history
Made the time formatting a bit more simple and dynamic
  • Loading branch information
Artemis-the-gr8 authored Aug 17, 2022
2 parents 3a9eb86 + f238221 commit 13a557e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ public double getSeconds() {
};
}

/** 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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,76 +54,59 @@ public String formatDistanceNumber(long number, Unit statUnit) { //15 statistic
}

/** 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")) {
output.append(" ");

//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());
}
double hours = Math.floor(leftover / 60 / 60);
leftover = leftover % (60 * 60);
if (smallUnit == Unit.HOUR) {
if (leftover >= 1800) {
hours++;

//We did not have enough leftover to fill a unit
else{
if(output.toString().length() != 0){
output.append(" 0").append(currUnit.getShortLabel());
}
return output.append(format.format(hours))
.append("h").toString();
currUnit = currUnit.getSmallerUnit(1);
continue;
}
output.append(format.format(hours))
.append("h");
}
if (isInRange(max, min, 60) && leftover >= 60) {
if (output.toString().contains("h")) {

//Calculate new leftover
leftoverSeconds = leftoverSeconds - (int)(amount * currUnit.getSeconds());

//Append new values
if(output.toString().length() != 0){
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(" ");
output.append(amount).append(currUnit.getShortLabel());

//Check if we need to break
if(currUnit == smallestUnit || leftoverSeconds == 0){
break;
}
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;
return output.toString();
}
}

0 comments on commit 13a557e

Please sign in to comment.