fix announcement time incorrectness

This commit is contained in:
Lightczx
2023-11-10 14:20:44 +08:00
parent 26d23fec7f
commit f97bc344d0
8 changed files with 69 additions and 53 deletions

View File

@@ -42,18 +42,4 @@ internal static class DateTimeOffsetExtension
return defaultValue;
}
}
[SuppressMessage("", "SH002")]
public static unsafe DateTimeOffset UnsafeAdjustOffsetOnly(this DateTimeOffset dateTimeOffset, in TimeSpan offset)
{
UnsafeWritableDateTimeOffset* pUnsafe = (UnsafeWritableDateTimeOffset*)&dateTimeOffset;
pUnsafe->OffsetMinutes = (short)(offset.Ticks / TimeSpan.TicksPerMinute);
return dateTimeOffset;
}
private struct UnsafeWritableDateTimeOffset
{
public DateTime DateTime;
public short OffsetMinutes;
}
}

View File

@@ -0,0 +1,18 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Extension;
internal struct UnsafeDateTimeOffset
{
private DateTime dateTime;
private short offsetMinutes;
[SuppressMessage("", "SH002")]
public static unsafe DateTimeOffset AdjustOffsetOnly(DateTimeOffset dateTimeOffset, in TimeSpan offset)
{
UnsafeDateTimeOffset* pUnsafe = (UnsafeDateTimeOffset*)&dateTimeOffset;
pUnsafe->offsetMinutes = (short)(offset.Ticks / TimeSpan.TicksPerMinute);
return dateTimeOffset;
}
}

View File

@@ -34,6 +34,7 @@ internal sealed class UIGF : IJsonOnSerializing, IJsonOnDeserialized
public void OnSerializing()
{
TimeSpan offset = GetRegionTimeZoneUtcOffset();
foreach (UIGFItem item in List)
{
item.Time = item.Time.ToOffset(offset);
@@ -44,9 +45,10 @@ internal sealed class UIGF : IJsonOnSerializing, IJsonOnDeserialized
{
// Adjust items timezone
TimeSpan offset = GetRegionTimeZoneUtcOffset();
foreach (UIGFItem item in List)
{
item.Time = item.Time.UnsafeAdjustOffsetOnly(offset);
item.Time = UnsafeDateTimeOffset.AdjustOffsetOnly(item.Time, offset);
}
}

View File

@@ -9,6 +9,7 @@ namespace Snap.Hutao.Web.Hoyolab.Hk4e.Common.Announcement;
[HighQuality]
internal sealed class Announcement : AnnouncementContent
{
#region Binding
/// <summary>
/// 是否应展示时间
/// </summary>
@@ -81,6 +82,7 @@ internal sealed class Announcement : AnnouncementContent
{
get => $"{StartTime:yyyy.MM.dd HH:mm} - {EndTime:yyyy.MM.dd HH:mm}";
}
#endregion
/// <summary>
/// 类型标签

View File

@@ -9,7 +9,7 @@ namespace Snap.Hutao.Web.Hoyolab.Hk4e.Common.Announcement;
/// 公告包装器
/// </summary>
[HighQuality]
internal sealed class AnnouncementWrapper : ListWrapper<AnnouncementListWrapper>
internal sealed class AnnouncementWrapper : ListWrapper<AnnouncementListWrapper>, IJsonOnDeserialized
{
/// <summary>
/// 总数
@@ -46,4 +46,18 @@ internal sealed class AnnouncementWrapper : ListWrapper<AnnouncementListWrapper>
/// </summary>
[JsonPropertyName("t")]
public string TimeStamp { get; set; } = default!;
public void OnDeserialized()
{
TimeSpan offset = new(TimeZone, 0, 0);
foreach (AnnouncementListWrapper wrapper in List)
{
foreach (Announcement item in wrapper.List)
{
item.StartTime = UnsafeDateTimeOffset.AdjustOffsetOnly(item.StartTime, offset);
item.EndTime = UnsafeDateTimeOffset.AdjustOffsetOnly(item.EndTime, offset);
}
}
}
}

View File

@@ -2,7 +2,6 @@
// Licensed under the MIT license.
using System.Text.RegularExpressions;
using Windows.ApplicationModel.Store.Preview.InstallControl;
namespace Snap.Hutao.Web.Hoyolab;
@@ -62,7 +61,7 @@ internal readonly partial struct PlayerUid
};
}
public static TimeZoneInfo GetTimeZoneInfo(string uid)
public static TimeSpan GetRegionTimeZoneUtcOffset(string uid)
{
// We make this a static method rather than property,
// to avoid unnecessary memory allocation (Region field).
@@ -73,17 +72,12 @@ internal readonly partial struct PlayerUid
// 其他 UTC+08
return uid.AsSpan()[0] switch
{
'6' => ServerTimeZoneInfo.AmericaTimeZone,
'7' => ServerTimeZoneInfo.EuropeTimeZone,
_ => ServerTimeZoneInfo.CommonTimeZone,
'6' => ServerRegionTimeZone.AmericaServerOffset,
'7' => ServerRegionTimeZone.EuropeServerOffset,
_ => ServerRegionTimeZone.CommonOffset,
};
}
public static TimeSpan GetRegionTimeZoneUtcOffset(string uid)
{
return GetTimeZoneInfo(uid).BaseUtcOffset;
}
/// <inheritdoc/>
public override string ToString()
{
@@ -107,6 +101,6 @@ internal readonly partial struct PlayerUid
};
}
[GeneratedRegex("[1-9][0-9]{8}")]
[GeneratedRegex("^[1-9][0-9]{8}$")]
private static partial Regex UidRegex();
}

View File

@@ -0,0 +1,26 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Web.Hoyolab;
internal static class ServerRegionTimeZone
{
private static readonly TimeSpan AmericaOffsetValue = new(-05, 0, 0);
private static readonly TimeSpan EuropeOffsetValue = new(+01, 0, 0);
private static readonly TimeSpan CommonOffsetValue = new(+08, 0, 0);
/// <summary>
/// UTC-05
/// </summary>
public static TimeSpan AmericaServerOffset { get => AmericaOffsetValue; }
/// <summary>
/// UTC+01
/// </summary>
public static TimeSpan EuropeServerOffset { get => EuropeOffsetValue; }
/// <summary>
/// UTC+08
/// </summary>
public static TimeSpan CommonOffset { get => CommonOffsetValue; }
}

View File

@@ -1,26 +0,0 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.
namespace Snap.Hutao.Web.Hoyolab;
internal static class ServerTimeZoneInfo
{
private static readonly TimeZoneInfo AmericaTimeZoneValue = TimeZoneInfo.CreateCustomTimeZone("Server:UTC-05", new TimeSpan(-05, 0, 0), "UTC-05", "UTC-05");
private static readonly TimeZoneInfo EuropeTimeZoneValue = TimeZoneInfo.CreateCustomTimeZone("Server:UTC+01", new TimeSpan(+01, 0, 0), "UTC+01", "UTC+01");
private static readonly TimeZoneInfo CommonTimeZoneValue = TimeZoneInfo.CreateCustomTimeZone("Server:UTC+08", new TimeSpan(+08, 0, 0), "UTC+08", "UTC+08");
/// <summary>
/// UTC-05
/// </summary>
public static TimeZoneInfo AmericaTimeZone { get => AmericaTimeZoneValue; }
/// <summary>
/// UTC+01
/// </summary>
public static TimeZoneInfo EuropeTimeZone { get => EuropeTimeZoneValue; }
/// <summary>
/// UTC+08
/// </summary>
public static TimeZoneInfo CommonTimeZone { get => CommonTimeZoneValue; }
}