Skip to content

Commit db81146

Browse files
committed
fix(Time): 修正年份相关时间戳计算以使用当前时区
将 GetYearStartTime() 从使用 UTC 时间改为使用当前时区时间。 更新所有相关的时间戳计算方法(GetYearStartTimestamp、GetYearEndTimestamp、GetStartTimestampOfYear、GetEndTimestampOfYear),在创建 DateTimeOffset 时显式指定当前时区的偏移量,以确保时间戳计算与时区设置一致。
1 parent 0ce8e09 commit db81146

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,21 @@ public partial class TimerHelper
3838
/// </summary>
3939
/// <returns>本年1月1日零点时间</returns>
4040
/// <remarks>
41-
/// 此方法基于UTC时间计算年份:
42-
/// 1. 获取当前UTC时间的年份
41+
/// 此方法基于当前时区时间计算年份:
42+
/// 1. 获取当前时区时间的年份
4343
/// 2. 返回该年份1月1日零点时间
4444
///
4545
/// 示例:
46-
/// - 当前UTC时间为2024-03-15 14:30:00
46+
/// - 当前时间为2024-03-15 14:30:00
4747
/// - 返回2024-01-01 00:00:00
4848
///
4949
/// 注意:
50-
/// - 返回的是UTC时间,不考虑本地时区
50+
/// - 返回的是当前时区的时间
5151
/// - 返回时间的时分秒毫秒都为0
52-
/// - 使用DateTime.UtcNow避免时区转换带来的问题
5352
/// </remarks>
5453
public static DateTime GetYearStartTime()
5554
{
56-
return new DateTime(GetUtcNow().Year, 1, 1);
55+
return new DateTime(GetNow().Year, 1, 1);
5756
}
5857

5958
/// <summary>
@@ -62,12 +61,13 @@ public static DateTime GetYearStartTime()
6261
/// <returns>本年1月1日零点时间戳(秒)</returns>
6362
/// <remarks>
6463
/// 此方法返回当前年份1月1日零点的Unix时间戳
65-
/// 使用本地时区计算时间
64+
/// 使用当前时区计算时间
6665
/// 例如:2024年返回2024-01-01 00:00:00的时间戳
6766
/// </remarks>
6867
public static long GetYearStartTimestamp()
6968
{
70-
return new DateTimeOffset(GetYearStartTime()).ToUnixTimeSeconds();
69+
var time = GetYearStartTime();
70+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
7171
}
7272

7373
/// <summary>
@@ -76,7 +76,7 @@ public static long GetYearStartTimestamp()
7676
/// <returns>本年12月31日23:59:59的时间</returns>
7777
/// <remarks>
7878
/// 此方法返回当前年份最后一天的最后一秒
79-
/// 使用本地时区计算时间
79+
/// 使用当前时区计算时间
8080
/// 例如:2024年返回2024-12-31 23:59:59
8181
/// </remarks>
8282
public static DateTime GetYearEndTime()
@@ -90,12 +90,13 @@ public static DateTime GetYearEndTime()
9090
/// <returns>本年12月31日23:59:59的时间戳(秒)</returns>
9191
/// <remarks>
9292
/// 此方法返回当前年份最后一天的最后一秒的Unix时间戳
93-
/// 使用本地时区计算时间
93+
/// 使用当前时区计算时间
9494
/// 例如:2024年返回2024-12-31 23:59:59的时间戳
9595
/// </remarks>
9696
public static long GetYearEndTimestamp()
9797
{
98-
return new DateTimeOffset(GetYearEndTime()).ToUnixTimeSeconds();
98+
var time = GetYearEndTime();
99+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
99100
}
100101

101102
/// <summary>
@@ -125,7 +126,8 @@ public static DateTime GetStartTimeOfYear(DateTime date)
125126
/// </remarks>
126127
public static long GetStartTimestampOfYear(DateTime date)
127128
{
128-
return new DateTimeOffset(GetStartTimeOfYear(date)).ToUnixTimeSeconds();
129+
var time = GetStartTimeOfYear(date);
130+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
129131
}
130132

131133
/// <summary>
@@ -155,6 +157,7 @@ public static DateTime GetEndTimeOfYear(DateTime date)
155157
/// </remarks>
156158
public static long GetEndTimestampOfYear(DateTime date)
157159
{
158-
return new DateTimeOffset(GetEndTimeOfYear(date)).ToUnixTimeSeconds();
160+
var time = GetEndTimeOfYear(date);
161+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
159162
}
160163
}

0 commit comments

Comments
 (0)