Skip to content

Commit

Permalink
Merge pull request #971 from lrusso96/patch-1
Browse files Browse the repository at this point in the history
[YouTube] Improve duration parsing
  • Loading branch information
TobiGr authored Nov 16, 2022
2 parents f6eefdc + c963521 commit 2211a24
Showing 1 changed file with 10 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,38 +316,16 @@ public static int parseDurationString(@Nonnull final String input)
? input.split(":")
: input.split("\\.");

String days = "0";
String hours = "0";
String minutes = "0";
final String seconds;

switch (splitInput.length) {
case 4:
days = splitInput[0];
hours = splitInput[1];
minutes = splitInput[2];
seconds = splitInput[3];
break;
case 3:
hours = splitInput[0];
minutes = splitInput[1];
seconds = splitInput[2];
break;
case 2:
minutes = splitInput[0];
seconds = splitInput[1];
break;
case 1:
seconds = splitInput[0];
break;
default:
throw new ParsingException("Error duration string with unknown format: " + input);
}

return ((convertDurationToInt(days) * 24
+ convertDurationToInt(hours)) * 60
+ convertDurationToInt(minutes)) * 60
+ convertDurationToInt(seconds);
final int[] units = {24, 60, 60, 1};
final int offset = units.length - splitInput.length;
if (offset < 0) {
throw new ParsingException("Error duration string with unknown format: " + input);
}
int duration = 0;
for (int i = 0; i < splitInput.length; i++) {
duration = units[i + offset] * (duration + convertDurationToInt(splitInput[i]));
}
return duration;
}

/**
Expand Down

0 comments on commit 2211a24

Please sign in to comment.