1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-11-23 10:39:26 +01:00

Merge pull request #11244 from Isira-Seneviratne/Android-elapsed-time

Use Android's elapsed time formatting
This commit is contained in:
Stypox 2024-11-18 16:56:41 +01:00 committed by GitHub
commit 06d25b0310
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -10,6 +10,7 @@ import android.content.res.Resources;
import android.icu.text.CompactDecimalFormat;
import android.os.Build;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import androidx.annotation.NonNull;
@ -239,43 +240,27 @@ public final class Localization {
}
/**
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds}.
* Prepended zeros are removed.
* Get a readable text for a duration in the format {@code hours:minutes:seconds}.
*
* @param duration the duration in seconds
* @return a formatted duration String or {@code 0:00} if the duration is zero.
* @return a formatted duration String or {@code 00:00} if the duration is zero.
*/
public static String getDurationString(final long duration) {
return getDurationString(duration, true, false);
return DateUtils.formatElapsedTime(Math.max(duration, 0));
}
/**
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds+}.
* Prepended zeros are removed. If the given duration is incomplete, a plus is appended to the
* duration string.
* Get a readable text for a duration in the format {@code hours:minutes:seconds+}. If the given
* duration is incomplete, a plus is appended to the duration string.
*
* @param duration the duration in seconds
* @param isDurationComplete whether the given duration is complete or whether info is missing
* @param showDurationPrefix whether the duration-prefix shall be shown
* @return a formatted duration String or {@code 0:00} if the duration is zero.
* @return a formatted duration String or {@code 00:00} if the duration is zero.
*/
public static String getDurationString(final long duration, final boolean isDurationComplete,
final boolean showDurationPrefix) {
final String output;
final long days = duration / (24 * 60 * 60L); /* greater than a day */
final long hours = duration % (24 * 60 * 60L) / (60 * 60L); /* greater than an hour */
final long minutes = duration % (24 * 60 * 60L) % (60 * 60L) / 60L;
final long seconds = duration % 60L;
if (duration < 0) {
output = "0:00";
} else if (days > 0) {
//handle days
output = String.format(Locale.US, "%d:%02d:%02d:%02d", days, hours, minutes, seconds);
} else if (hours > 0) {
output = String.format(Locale.US, "%d:%02d:%02d", hours, minutes, seconds);
} else {
output = String.format(Locale.US, "%d:%02d", minutes, seconds);
}
final String output = getDurationString(duration);
final String durationPrefix = showDurationPrefix ? "" : "";
final String durationPostfix = isDurationComplete ? "" : "+";
return durationPrefix + output + durationPostfix;