Skip to content

Commit 963dbe7

Browse files
committed
fix(time): 修复输入验证和 DateTimeKind 问题
- GetCrossDays 方法添加 hour 参数范围验证 (0-23) - GetMonthStartTimeWithTimeZone 方法将 DateTimeKind.Local 改为 Unspecified
1 parent 8a5531c commit 963dbe7

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

GameFrameX.Foundation.Utility/Time/TimerHelper.Day.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
// Official Documentation: https://gameframex.doc.alianblank.com/
3030
// ==========================================================================================
3131

32+
using GameFrameX.Foundation.Localization.Core;
33+
using GameFrameX.Foundation.Utility.Localization;
34+
3235
namespace GameFrameX.Foundation.Utility;
3336

3437
public static partial class TimerHelper
@@ -94,8 +97,9 @@ public static double GetDaysDifference(DateTime startTime, DateTime endTime)
9497
/// </summary>
9598
/// <param name="startTime">起始时间。</param>
9699
/// <param name="endTime">结束时间。</param>
97-
/// <param name="hour">用于判定跨日的小时阈值。</param>
100+
/// <param name="hour">用于判定跨日的小时阈值,有效范围为 0-23。</param>
98101
/// <returns>跨越的天数。</returns>
102+
/// <exception cref="ArgumentOutOfRangeException">当 <paramref name="hour"/> 不在 0-23 范围内时抛出。</exception>
99103
/// <remarks>
100104
/// 此方法先计算两个日期(忽略具体时间部分)之间的天数差,再依据小时阈值调整结果。
101105
/// 如果 <paramref name="startTime"/> 的小时数小于阈值,则结果加一;
@@ -111,6 +115,11 @@ public static double GetDaysDifference(DateTime startTime, DateTime endTime)
111115
/// </example>
112116
public static int GetCrossDays(DateTime startTime, DateTime endTime, int hour = 0)
113117
{
118+
if (hour < 0 || hour > 23)
119+
{
120+
throw new ArgumentOutOfRangeException(nameof(hour), hour, LocalizationService.GetString(LocalizationKeys.Exceptions.HourOutOfRange));
121+
}
122+
114123
var days = (int)(endTime.Date - startTime.Date).TotalDays;
115124
if (startTime.Hour < hour)
116125
{

GameFrameX.Foundation.Utility/Time/TimerHelper.Month.TimeZone.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static partial class TimerHelper
4444
public static DateTime GetMonthStartTimeWithTimeZone()
4545
{
4646
var now = GetNowWithTimeZone();
47-
return new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Local);
47+
return new DateTime(now.Year, now.Month, 1, 0, 0, 0, DateTimeKind.Unspecified);
4848
}
4949

5050
/// <summary>

0 commit comments

Comments
 (0)