兼容特殊时间格式

This commit is contained in:
zpc 2025-11-16 17:25:46 +08:00
parent 06648a270b
commit 0bbfa0ba32
2 changed files with 87 additions and 6 deletions

View File

@ -762,7 +762,8 @@ namespace ZR.Admin.WebApi.Controllers.Business
/// </summary> /// </summary>
/// <param name="historyRemarks">原始字符串</param> /// <param name="historyRemarks">原始字符串</param>
/// <returns>日期+原因集合</returns> /// <returns>日期+原因集合</returns>
private List<(DateTime Date, string Reason)> ParseHistory(string historyRemarks) [Obsolete]
private List<(DateTime Date, string Reason)> ParseHistoryf(string historyRemarks)
{ {
var result = new List<(DateTime, string)>(); var result = new List<(DateTime, string)>();
@ -801,6 +802,86 @@ namespace ZR.Admin.WebApi.Controllers.Business
return result; return result;
} }
/// <summary>
/// 超强健壮型历史备注解析器,支持以下所有日期格式:
/// - 英文2022/5/3, 2022-10 24, 2022-10-24, 2021/ 12/2
/// - 中文2022年5月3日, 2022年5月3号 9:20
/// - 时间:支持无秒,如 8:20、8:5、8:5:1、自动补零
/// - 自动修复多余空格
///
/// 最终统一输出 yyyy-MM-dd HH:mm:ss
/// </summary>
private List<(DateTime Date, string Reason)> ParseHistory(string historyRemarks)
{
var list = new List<(DateTime, string)>();
if (string.IsNullOrWhiteSpace(historyRemarks))
return list;
// 支持:
// - yyyy/MM/dd
// - yyyy- MM dd
// - yyyy年M月d日
// - yyyy年M月d号
// - 时间可为 HH:mm 或 HH:mm:ss
var regex = new Regex(
@"(?<year>\d{4})\s*(?:[-/年]\s*)?(?<month>\d{1,2})\s*(?:[-/月]\s*)?(?<day>\d{1,2})\s*(?:日|号)?\s+(?<time>\d{1,2}:\d{1,2}(?::\d{1,2})?)",
RegexOptions.Compiled);
string[] lines = historyRemarks.Split(new[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in lines)
{
var m = regex.Match(line);
if (!m.Success)
continue;
try
{
// 年月日
int year = int.Parse(m.Groups["year"].Value);
int month = int.Parse(m.Groups["month"].Value);
int day = int.Parse(m.Groups["day"].Value);
// 时间部分处理(支持补全)
string timeRaw = m.Groups["time"].Value.Trim();
string time = NormalizeTime(timeRaw);
// 拼成字符串再解析
string dateStr = $"{year:D4}-{month:D2}-{day:D2} {time}";
if (DateTime.TryParse(dateStr, out var dt))
{
// 提取事件原因(时间之后全部文字)
string reason = line.Substring(m.Index + m.Length).Trim();
list.Add((dt, reason));
}
}
catch
{
// ignore
}
}
return list;
}
/// <summary>
/// 时间字符串标准化:
/// - 8:5 → 08:05:00
/// - 8:5:3 → 08:05:03
/// - 8:20 → 08:20:00
/// </summary>
private string NormalizeTime(string rawTime)
{
var parts = rawTime.Split(':');
string h = parts[0].PadLeft(2, '0');
string m = parts[1].PadLeft(2, '0');
string s = parts.Length >= 3 ? parts[2].PadLeft(2, '0') : "00";
return $"{h}:{m}:{s}";
}
/// <summary> /// <summary>
/// 端口导入模板下载 /// 端口导入模板下载

View File

@ -1,4 +1,4 @@
{ {
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {
"Default": "Information", "Default": "Information",
@ -14,7 +14,7 @@
// "IsAutoCloseConnection": true // "IsAutoCloseConnection": true
//} //}
{ {
"Conn": "Data Source=192.168.195.8;User ID=sa;Password=Dbt@com@123;Initial Catalog=OdtAdmin;Encrypt=True;TrustServerCertificate=True;", "Conn": "Data Source=192.168.195.15;User ID=sa;Password=Dbt@com@123;Initial Catalog=OdtAdmin;Encrypt=True;TrustServerCertificate=True;",
"DbType": 1, // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4 "DbType": 1, // MySql = 0, SqlServer = 1, Oracle = 3PgSql = 4
"ConfigId": "0", // "ConfigId": "0", //
"IsAutoCloseConnection": true "IsAutoCloseConnection": true
@ -23,7 +23,7 @@
// http://localhost:8888/common/initseedData // http://localhost:8888/common/initseedData
"CodeGenDbConfig": { "CodeGenDbConfig": {
//{dbName} //{dbName}
"Conn": "Data Source=192.168.195.8;User ID=sa;Password=Dbt@com@123;Encrypt=True;TrustServerCertificate=True;Initial Catalog={dbName};", "Conn": "Data Source=192.168.195.15;User ID=sa;Password=Dbt@com@123;Encrypt=True;TrustServerCertificate=True;Initial Catalog={dbName};",
"DbType": 1, "DbType": 1,
"IsAutoCloseConnection": true, "IsAutoCloseConnection": true,
"DbName": "OdtAdmin" //,Oracle "DbName": "OdtAdmin" //,Oracle
@ -99,8 +99,8 @@
"RedisServer": { "RedisServer": {
"open": 0, //redis "open": 0, //redis
"dbCache": false, //使Redisopen1 "dbCache": false, //使Redisopen1
"Cache": "192.168.1.41:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:", "Cache": "192.168.1.15:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=cache:",
"Session": "192.168.1.41:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:" "Session": "192.168.1.15:6379,defaultDatabase=0,poolsize=50,ssl=false,writeBuffer=10240,prefix=session:"
}, },
// //
"CaptchaOptions": { "CaptchaOptions": {