自习室优化ok
This commit is contained in:
@@ -33,21 +33,41 @@ String formatDate(dynamic date, [String format = 'YYYY-MM-DD hh:mm:ss']) {
|
||||
|
||||
/// 将秒数格式化为 00:00 或 00:00:00
|
||||
/// - [seconds]: 秒数
|
||||
String formatSeconds(int seconds) {
|
||||
final h = seconds ~/ 3600;
|
||||
final m = (seconds % 3600) ~/ 60;
|
||||
final s = seconds % 60;
|
||||
/// - [format]: 格式化字符串,默认为 "hh:mm:ss"
|
||||
String formatSeconds(
|
||||
int seconds, [
|
||||
String format = 'hh:mm:ss',
|
||||
]) {
|
||||
if (seconds < 0) seconds = 0;
|
||||
|
||||
String twoDigits(int n) => n.toString().padLeft(2, '0');
|
||||
int h = seconds ~/ 3600;
|
||||
int m = (seconds % 3600) ~/ 60;
|
||||
int s = seconds % 60;
|
||||
|
||||
if (h > 0) {
|
||||
return '${twoDigits(h)}:${twoDigits(m)}:${twoDigits(s)}';
|
||||
} else {
|
||||
return '${twoDigits(m)}:${twoDigits(s)}';
|
||||
}
|
||||
String two(int n) => n.toString().padLeft(2, '0');
|
||||
|
||||
// 支持以下 token:
|
||||
// hh = 补零小时, h = 不补零小时
|
||||
// mm = 补零分钟, m = 不补零分钟
|
||||
// ss = 补零秒, s = 不补零秒
|
||||
final replacements = {
|
||||
'hh': two(h),
|
||||
'mm': two(m),
|
||||
'ss': two(s),
|
||||
'h': h.toString(),
|
||||
'm': m.toString(),
|
||||
's': s.toString(),
|
||||
};
|
||||
|
||||
String result = format;
|
||||
|
||||
replacements.forEach((key, value) {
|
||||
result = result.replaceAll(key, value);
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
/// 将 "HH", "HH:mm" 或 "HH:mm:ss" 转为当天 DateTime
|
||||
DateTime parseTime(String timeStr) {
|
||||
final now = DateTime.now();
|
||||
|
||||
Reference in New Issue
Block a user