more announcement time fix

This commit is contained in:
Lightczx
2023-12-21 15:57:09 +08:00
parent 4908364e45
commit 18d3180bc2
2 changed files with 11 additions and 6 deletions

View File

@@ -2,12 +2,20 @@
// Licensed under the MIT license.
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Runtime.CompilerServices;
namespace Snap.Hutao.Core;
internal static class UnsafeDateTimeOffset
{
[SuppressMessage("", "SH002")]
public static DateTimeOffset ParseDateTime(ReadOnlySpan<char> span, TimeSpan offset)
{
DateTime dateTime = DateTime.Parse(span, CultureInfo.InvariantCulture);
return new(dateTime, offset);
}
[Pure]
[SuppressMessage("", "SH002")]
public static unsafe DateTimeOffset AdjustOffsetOnly(DateTimeOffset dateTimeOffset, in TimeSpan offset)

View File

@@ -110,8 +110,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
return;
}
DateTimeOffset rawVersionUpdateTime = DateTimeOffset.Parse(versionMatch.Groups[1].ValueSpan, CultureInfo.InvariantCulture);
DateTimeOffset versionUpdateTime = UnsafeDateTimeOffset.AdjustOffsetOnly(rawVersionUpdateTime, offset);
DateTimeOffset versionUpdateTime = UnsafeDateTimeOffset.ParseDateTime(versionMatch.Groups[1].ValueSpan, offset);
foreach (ref readonly Announcement announcement in CollectionsMarshal.AsSpan(activities))
{
@@ -131,8 +130,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
if (AnnouncementRegex.TransientActivityAfterUpdateTimeRegex.Match(announcement.Content) is { Success: true } transient)
{
announcement.StartTime = versionUpdateTime;
DateTimeOffset rawEndTime = DateTimeOffset.Parse(transient.Groups[2].ValueSpan, CultureInfo.InvariantCulture);
announcement.EndTime = UnsafeDateTimeOffset.AdjustOffsetOnly(rawEndTime, offset);
announcement.EndTime = UnsafeDateTimeOffset.ParseDateTime(transient.Groups[2].ValueSpan, offset);
continue;
}
@@ -145,8 +143,7 @@ internal sealed partial class AnnouncementService : IAnnouncementService
List<DateTimeOffset> dateTimes = [];
foreach (Match timeMatch in (IList<Match>)matches)
{
DateTimeOffset raw = DateTimeOffset.Parse(timeMatch.Groups[1].ValueSpan, CultureInfo.InvariantCulture);
dateTimes.Add(UnsafeDateTimeOffset.AdjustOffsetOnly(raw, offset));
dateTimes.Add(UnsafeDateTimeOffset.ParseDateTime(timeMatch.Groups[1].ValueSpan, offset));
}
DateTimeOffset min = DateTimeOffset.MaxValue;