107 lines
3.1 KiB
Dart
107 lines
3.1 KiB
Dart
// 时区转换函数
|
||
void applyTimezoneConversion(Map<String, dynamic> data) {
|
||
final dynamic timeZoneValue = data['utc'];
|
||
if (timeZoneValue == null) return;
|
||
|
||
// 解析目标时区
|
||
final int? targetOffset = _parseTimezone(timeZoneValue);
|
||
if (targetOffset == null) return;
|
||
|
||
// 原始数据是UTC+8的时间戳
|
||
const int sourceOffset = 8; // UTC+8
|
||
final int diffHours = targetOffset - sourceOffset;
|
||
final int diffMs = diffHours * 3600 * 1000;
|
||
|
||
// 转换startTime和endTime
|
||
if (data['startTime'] != null && data['startTime'] is int) {
|
||
data['startTime'] = data['startTime'] + diffMs;
|
||
}
|
||
if (data['endTime'] != null && data['endTime'] is int) {
|
||
data['endTime'] = data['endTime'] + diffMs;
|
||
}
|
||
|
||
// 递归转换所有时间戳,但要跳过hrsp数组
|
||
_convertTimestampsRecursive(data, diffMs, skipField: 'hrsp');
|
||
}
|
||
|
||
// 解析时区值 - 支持多种类型
|
||
int? _parseTimezone(dynamic timeZoneValue) {
|
||
if (timeZoneValue == null) return null;
|
||
|
||
// 如果是整数类型
|
||
if (timeZoneValue is int) {
|
||
return timeZoneValue;
|
||
}
|
||
|
||
// 如果是字符串类型
|
||
if (timeZoneValue is String) {
|
||
// 移除空白字符
|
||
final String trimmed = timeZoneValue.trim();
|
||
|
||
// 处理纯数字字符串,如 "9"、"-2"
|
||
if (RegExp(r'^[+-]?\d+$').hasMatch(trimmed)) {
|
||
return int.parse(trimmed);
|
||
}
|
||
|
||
// 处理带UTC前缀的字符串,如 "utc+9"、"UTC-2"
|
||
final match =
|
||
RegExp(r'utc([+-]?\d+)', caseSensitive: false).firstMatch(trimmed);
|
||
if (match != null) {
|
||
return int.parse(match.group(1)!);
|
||
}
|
||
|
||
// 处理其他格式,如 "+9"、"-2"
|
||
if (trimmed.startsWith('+') || trimmed.startsWith('-')) {
|
||
return int.parse(trimmed);
|
||
}
|
||
}
|
||
|
||
// 如果是数字类型(double等)
|
||
if (timeZoneValue is num) {
|
||
return timeZoneValue.toInt();
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
// 递归转换所有时间戳,可指定跳过的字段
|
||
void _convertTimestampsRecursive(dynamic data, int diffMs, {String? skipField}) {
|
||
if (data is Map) {
|
||
// 如果不是跳过的字段类型,转换时间戳
|
||
if (!(skipField != null && data.containsKey(skipField))) {
|
||
_convertMapTimestamps(data, diffMs);
|
||
}
|
||
|
||
// 递归处理所有子元素
|
||
data.forEach((key, value) {
|
||
if (value is Map || value is List) {
|
||
// 如果当前key是要跳过的字段,则不深入处理其内容
|
||
if (skipField != null && key == skipField) {
|
||
return; // 跳过hrsp数组的处理
|
||
}
|
||
_convertTimestampsRecursive(value, diffMs, skipField: skipField);
|
||
}
|
||
});
|
||
} else if (data is List) {
|
||
for (final item in data) {
|
||
if (item is Map || item is List) {
|
||
_convertTimestampsRecursive(item, diffMs, skipField: skipField);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 转换map中的时间戳字段
|
||
void _convertMapTimestamps(Map map, int diffMs) {
|
||
// 要转换的字段名
|
||
const timestampFields = ['startTime', 'endTime', 'st', 'et'];
|
||
|
||
for (final field in timestampFields) {
|
||
if (map.containsKey(field) && map[field] != null) {
|
||
final value = map[field];
|
||
if (value is int) {
|
||
map[field] = value + diffMs;
|
||
}
|
||
}
|
||
}
|
||
} |