refactor:时区格式化提取DateTimeHelper便于复用

This commit is contained in:
hardstylewyl 2025-03-26 17:26:41 +08:00
parent 0b85005b57
commit 3ca5e538de
4 changed files with 27 additions and 53 deletions

View File

@ -55,4 +55,25 @@ public static class DateTimeHelper
return sign * (hours + minutes / 60f);
}
/// <summary>
/// 将浮点数时区转换为GMT格式
/// 如果为null则返回"--"
/// </summary>
/// <param name="timezone"></param>
/// <returns></returns>
public static string ConvertFloatToTimeZone(float? timezone)
{
if (timezone == null) return "--";
if (Math.Abs(timezone.Value - GlobalConst.NoneTrackTimeZone) < 1) return "--";
// 将小数形式的小时转换为时区偏移量
var hours = (int) timezone.Value; // 提取整数部分作为小时
var minutes = (int) ((timezone.Value - hours) * 60); // 将小数部分乘以 60 转换为分钟
var offset = new TimeSpan(hours, minutes, 0);
var timeZoneStr = offset.ToString();
var flag = timezone > 0f ? "+" : "";
return $"GMT {flag}{timeZoneStr.RemovePostFix(":00")}";
}
}

View File

@ -186,7 +186,7 @@ public class CompleteWarningService : ApplicationService
LatestTrackContent = orderIdTrack?.TrackContent,
LatestTrackPlace = orderIdTrack?.TrackPlace,
LatestTrackTime = orderIdTrack?.TrackTime,
LatestTrackTimeZone = FormatTimeZone(orderIdTrack?.TrackTimeZone),
LatestTrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(orderIdTrack?.TrackTimeZone),
NotifyUserNames = ToUserNames(x.NotifyUsers),
CreateTime = x.CreateTime,
@ -229,22 +229,6 @@ public class CompleteWarningService : ApplicationService
}).ToList()
}).ToList();
}
// 格式化时区
string FormatTimeZone(float? timezone)
{
if (timezone == null) return "--";
if (Math.Abs(timezone.Value - GlobalConst.NoneTrackTimeZone) < 1) return "--";
// 将小数形式的小时转换为时区偏移量
var hours = (int) timezone.Value; // 提取整数部分作为小时
var minutes = (int) ((timezone.Value - hours) * 60); // 将小数部分乘以 60 转换为分钟
var offset = new TimeSpan(hours, minutes, 0);
var timeZoneStr = offset.ToString();
var flag = timezone > 0f ? "+" : "";
return $"GMT {flag}{timeZoneStr.RemovePostFix(":00")}";
}
}
/// <summary>

View File

@ -3,6 +3,7 @@ using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.Warning;
using SJZY.Track.Application.Contract.Dtos.Warning.Return;
using SJZY.Track.Application.Contract.Events;
using SJZY.Track.Application.Helpers;
using SJZY.Track.Application.Services.Warning.Handlers;
using SJZY.Track.Application.Services.Warning.Model;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
@ -146,7 +147,7 @@ public class ReturnWarningService : ApplicationService
TrackContent = x.TrackContent,
TrackPlace = x.TrackPlace,
TrackTime = x.TrackTime,
TrackTimeZone = FormatTimeZone(x.TrackTimeZone),
TrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(x.TrackTimeZone),
TrackCreateTime = x.TrackCreateTime,
// 轨迹信息(映射后)
PushTrackCode = x.PushTrackCode,
@ -186,22 +187,6 @@ public class ReturnWarningService : ApplicationService
return string.Join(',', notifyUsers.ToObject<List<NotifyUserDto>>().Select(x => x.UserName));
}
// 格式化时区
string FormatTimeZone(float? timezone)
{
if (timezone == null) return "--";
if (Math.Abs(timezone.Value - GlobalConst.NoneTrackTimeZone) < 1) return "--";
// 将小数形式的小时转换为时区偏移量
var hours = (int) timezone.Value; // 提取整数部分作为小时
var minutes = (int) ((timezone.Value - hours) * 60); // 将小数部分乘以 60 转换为分钟
var offset = new TimeSpan(hours, minutes, 0);
var timeZoneStr = offset.ToString();
var flag = timezone > 0f ? "+" : "";
return $"GMT {flag}{timeZoneStr.RemovePostFix(":00")}";
}
// 时间
string FormatTimeSpan(long? milliseconds)
{

View File

@ -223,20 +223,20 @@ public class TransportWarningService : ApplicationService
// 最新轨迹 (不冗余)
LatestTrackContent = orderIdTrack?.TrackContent,
LatestTrackTime = orderIdTrack?.TrackTime,
LatestTrackTimeZone = FormatTimeZone(orderIdTrack?.TrackTimeZone),
LatestTrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(orderIdTrack?.TrackTimeZone),
LatestTrackPlace = orderIdTrack?.TrackPlace,
// 起始轨迹
StartTrackStatus = x.StartTrackStatus,
StartTrackContent = x.StartTrackContent,
StartTrackTime = x.StartTrackTime,
StartTrackTimeZone = FormatTimeZone(x.StartTrackTimeZone),
StartTrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(x.StartTrackTimeZone),
// 截止轨迹
EndTrackStatus = x.EndTrackStatus,
EndTrackContent = x.EndTrackContent,
EndTrackTime = x.EndTrackTime,
EndTrackTimeZone = FormatTimeZone(x.EndTrackTimeZone),
EndTrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(x.EndTrackTimeZone),
WarningName = x.WarningName,
WarningRuleDescription = x.WarningRuleDescription,
@ -283,22 +283,6 @@ public class TransportWarningService : ApplicationService
return $"{timeSpan.Days:00}天{timeSpan.Hours:00}小时{timeSpan.Minutes:00}分钟";
}
// 格式化时区
string FormatTimeZone(float? timezone)
{
if (timezone == null) return "--";
if (Math.Abs(timezone.Value - GlobalConst.NoneTrackTimeZone) < 1) return "--";
// 将小数形式的小时转换为时区偏移量
var hours = (int) timezone.Value; // 提取整数部分作为小时
var minutes = (int) ((timezone.Value - hours) * 60); // 将小数部分乘以 60 转换为分钟
var offset = new TimeSpan(hours, minutes, 0);
var timeZoneStr = offset.ToString();
var flag = timezone > 0f ? "+" : "";
return $"GMT {flag}{timeZoneStr.RemovePostFix(":00")}";
}
}
/// <summary>