formatDuration static method
Implementation
static String formatDuration({int? startTime, int? endTime, int? second}) {
int seconds = 0;
if (second != null) {
seconds = second;
} else if (startTime != null && endTime != null) {
if (startTime <= 0 || endTime <= 0 || endTime < startTime) {
return "-";
}
seconds = endTime - startTime;
} else {
// 参数不合法
return "-";
}
if (seconds <= 0) {
return "-";
}
seconds = seconds ~/ 1000;
int days = seconds ~/ (24 * 3600);
int hours = (seconds % (24 * 3600)) ~/ 3600;
int minutes = (seconds % 3600) ~/ 60;
String result = '';
if (days > 0) result += '$days天';
if (hours > 0) result += '$hours小时';
if (minutes > 0) result += '$minutes分钟';
return result.isNotEmpty ? result : '0分钟'; // 确保返回值不会为空
}