Files
MEIZU-Battery-Healthy/lib/utils/time_utils.dart
T

49 lines
1.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:developer' as dev;
class TimeUtils {
/// 解析日志行中的时间戳,强制解析为 UTC
static DateTime? parseLogTimestamp(String line) {
final reg = RegExp(r'\[(\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2})\]');
final match = reg.firstMatch(line);
if (match == null) return null;
try {
final dateParts = match.group(1)!.split('-');
final timeParts = match.group(2)!.split(':');
return DateTime.utc(
int.parse(dateParts[0]),
int.parse(dateParts[1]),
int.parse(dateParts[2]),
int.parse(timeParts[0]),
int.parse(timeParts[1]),
int.parse(timeParts[2]),
);
} catch (_) {
return null;
}
}
/// 判断日志时间戳是否在有效窗口内
static bool isLogTimestampValid(
DateTime? logTime,
DateTime? chargingStartTime,
) {
if (logTime == null) return false;
if (chargingStartTime == null) {
final nowUtc = DateTime.now().toUtc();
final diff = nowUtc.difference(logTime);
return diff.abs().inMinutes <= 3;
}
final endTime = chargingStartTime.add(const Duration(minutes: 3));
return logTime.isAfter(chargingStartTime) && logTime.isBefore(endTime);
}
/// 生成今日日志文件名(UTC
static String getTodayFileName() {
final nowUtc = DateTime.now().toUtc();
final year = nowUtc.year.toString();
final month = nowUtc.month.toString().padLeft(2, '0');
final day = nowUtc.day.toString().padLeft(2, '0');
return 'mbattery_charger_log_${year}_${month}_$day';
}
}