mirror of
https://github.com/kodjodevf/mangayomi.git
synced 2026-01-11 22:40:36 +00:00
44 lines
1.7 KiB
Dart
44 lines
1.7 KiB
Dart
/// This file is a part of media_kit (https://github.com/media-kit/media-kit).
|
|
///
|
|
/// Copyright © 2021 & onwards, Hitesh Kumar Saini <saini123hitesh@gmail.com>.
|
|
/// All rights reserved.
|
|
/// Use of this source code is governed by MIT license that can be found in the LICENSE file.
|
|
|
|
// ignore_for_file: dangling_library_doc_comments
|
|
|
|
/// Extension methods for [Duration].
|
|
extension DurationExtension on Duration {
|
|
/// Returns clamp of [Duration] between [min] and [max].
|
|
Duration clamp(Duration min, Duration max) {
|
|
if (this < min) return min;
|
|
if (this > max) return max;
|
|
return this;
|
|
}
|
|
|
|
/// Returns a [String] representation of [Duration].
|
|
String label({Duration? reference}) {
|
|
reference ??= this;
|
|
reference = reference.abs();
|
|
|
|
if (isNegative) {
|
|
return abs().label(reference: reference);
|
|
}
|
|
|
|
if (reference > const Duration(days: 1)) {
|
|
final days = inDays.toString().padLeft(3, '0');
|
|
final hours = (inHours - (inDays * 24)).toString().padLeft(2, '0');
|
|
final minutes = (inMinutes - (inHours * 60)).toString().padLeft(2, '0');
|
|
final seconds = (inSeconds - (inMinutes * 60)).toString().padLeft(2, '0');
|
|
return '$days:$hours:$minutes:$seconds';
|
|
} else if (reference > const Duration(hours: 1)) {
|
|
final hours = inHours.toString().padLeft(2, '0');
|
|
final minutes = (inMinutes - (inHours * 60)).toString().padLeft(2, '0');
|
|
final seconds = (inSeconds - (inMinutes * 60)).toString().padLeft(2, '0');
|
|
return '$hours:$minutes:$seconds';
|
|
} else {
|
|
final minutes = inMinutes.toString().padLeft(2, '0');
|
|
final seconds = (inSeconds - (inMinutes * 60)).toString().padLeft(2, '0');
|
|
return '$minutes:$seconds';
|
|
}
|
|
}
|
|
}
|