From dd1d041e3fb7200dfa9d9e044ec55d960cc27c54 Mon Sep 17 00:00:00 2001 From: hardstylewyl <827778732@qq.com> Date: Fri, 11 Apr 2025 15:35:41 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E6=8F=90?= =?UTF-8?q?=E4=BE=9B=E6=8E=A5=E5=8F=A3=E8=BD=A8=E8=BF=B9=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E6=A0=B9=E6=8D=AE=E8=BD=AC=E5=8C=96=E5=90=8E?= =?UTF-8?q?=E7=9A=84utc0=E6=9D=A5=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helpers/DateTimeHelper.cs | 20 +++++++++++++++++++ .../ProvideServices/TrackService.Query.cs | 5 +++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs b/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs index c92202a4..c58fe83d 100644 --- a/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs +++ b/src/SJZY.Track.Application/Helpers/DateTimeHelper.cs @@ -78,4 +78,24 @@ public static class DateTimeHelper var flag = timezone > 0f ? "+" : ""; return $"GMT {flag}{timeZoneStr.RemovePostFix(":00")}"; } + + /// + /// 转换为utc0时区的时间转化进行Compare + /// + public static int ConvertUtc0Compare(DateTime a, float aZone, DateTime b, float bZone) + { + if (Math.Abs(aZone - bZone) < 0.01f) + { + return a.CompareTo(b); + } + + if (Math.Abs(aZone - GlobalConst.NoneTrackTimeZone) < 0.01f) + aZone = 8.0f; + if (Math.Abs(bZone - GlobalConst.NoneTrackTimeZone) < 0.01f) + bZone = 8.0f; + + var aUtc = a.Subtract(TimeSpan.FromHours(aZone)); + var bUtc = b.Subtract(TimeSpan.FromHours(bZone)); + return aUtc.CompareTo(bUtc); + } } diff --git a/src/SJZY.Track.Application/Services/ProvideServices/TrackService.Query.cs b/src/SJZY.Track.Application/Services/ProvideServices/TrackService.Query.cs index e5b184cb..caebb3ee 100644 --- a/src/SJZY.Track.Application/Services/ProvideServices/TrackService.Query.cs +++ b/src/SJZY.Track.Application/Services/ProvideServices/TrackService.Query.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Http; using SJZY.Expand.AliyunOSS; using SJZY.Track.Application.Contract.Dtos.ProvideServices; +using SJZY.Track.Application.Helpers; using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate; using SJZY.Track.Domain.Aggregates.TrackTemplateAggregate; using SJZY.Track.Domain.Dtos.TrackTemplate; @@ -201,12 +202,12 @@ public partial class TrackService case SortMethod.ByTrackTime: case SortMethod.ByTemplateAndTrackTime: // tips:quick-sort 直接修改原list - list.ForEach(item => item.TrackDetails.Sort((a, b) => a.Time.CompareTo(b.Time))); + list.ForEach(item => item.TrackDetails.Sort((a, b) => DateTimeHelper.ConvertUtc0Compare(a.Time, a.TimeZone ?? 8, b.Time, b.TimeZone ?? 8))); break; case null: case SortMethod.ByTrackTimeDesc: case SortMethod.ByTemplateAndTrackTimeDesc: - list.ForEach(item => item.TrackDetails.Sort((a, b) => b.Time.CompareTo(a.Time))); + list.ForEach(item => item.TrackDetails.Sort((a, b) => DateTimeHelper.ConvertUtc0Compare(b.Time, b.TimeZone ?? 8, a.Time, a.TimeZone ?? 8))); break; default: throw new ArgumentOutOfRangeException(nameof(sortMethod), sortMethod, "不支持的排序方式");