Skip to content

Commit 06d25b0

Browse files
authored
Merge pull request TeamNewPipe#11244 from Isira-Seneviratne/Android-elapsed-time
Use Android's elapsed time formatting
2 parents a962e6d + 07c63f7 commit 06d25b0

1 file changed

Lines changed: 10 additions & 25 deletions

File tree

app/src/main/java/org/schabi/newpipe/util/Localization.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.icu.text.CompactDecimalFormat;
1111
import android.os.Build;
1212
import android.text.TextUtils;
13+
import android.text.format.DateUtils;
1314
import android.util.DisplayMetrics;
1415

1516
import androidx.annotation.NonNull;
@@ -239,43 +240,27 @@ public static String likeCount(@NonNull final Context context, final int likeCou
239240
}
240241

241242
/**
242-
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds}.
243-
* Prepended zeros are removed.
243+
* Get a readable text for a duration in the format {@code hours:minutes:seconds}.
244+
*
244245
* @param duration the duration in seconds
245-
* @return a formatted duration String or {@code 0:00} if the duration is zero.
246+
* @return a formatted duration String or {@code 00:00} if the duration is zero.
246247
*/
247248
public static String getDurationString(final long duration) {
248-
return getDurationString(duration, true, false);
249+
return DateUtils.formatElapsedTime(Math.max(duration, 0));
249250
}
250251

251252
/**
252-
* Get a readable text for a duration in the format {@code days:hours:minutes:seconds+}.
253-
* Prepended zeros are removed. If the given duration is incomplete, a plus is appended to the
254-
* duration string.
253+
* Get a readable text for a duration in the format {@code hours:minutes:seconds+}. If the given
254+
* duration is incomplete, a plus is appended to the duration string.
255+
*
255256
* @param duration the duration in seconds
256257
* @param isDurationComplete whether the given duration is complete or whether info is missing
257258
* @param showDurationPrefix whether the duration-prefix shall be shown
258-
* @return a formatted duration String or {@code 0:00} if the duration is zero.
259+
* @return a formatted duration String or {@code 00:00} if the duration is zero.
259260
*/
260261
public static String getDurationString(final long duration, final boolean isDurationComplete,
261262
final boolean showDurationPrefix) {
262-
final String output;
263-
264-
final long days = duration / (24 * 60 * 60L); /* greater than a day */
265-
final long hours = duration % (24 * 60 * 60L) / (60 * 60L); /* greater than an hour */
266-
final long minutes = duration % (24 * 60 * 60L) % (60 * 60L) / 60L;
267-
final long seconds = duration % 60L;
268-
269-
if (duration < 0) {
270-
output = "0:00";
271-
} else if (days > 0) {
272-
//handle days
273-
output = String.format(Locale.US, "%d:%02d:%02d:%02d", days, hours, minutes, seconds);
274-
} else if (hours > 0) {
275-
output = String.format(Locale.US, "%d:%02d:%02d", hours, minutes, seconds);
276-
} else {
277-
output = String.format(Locale.US, "%d:%02d", minutes, seconds);
278-
}
263+
final String output = getDurationString(duration);
279264
final String durationPrefix = showDurationPrefix ? "⏱ " : "";
280265
final String durationPostfix = isDurationComplete ? "" : "+";
281266
return durationPrefix + output + durationPostfix;

0 commit comments

Comments
 (0)