Skip to content

Commit f668c15

Browse files
committed
fix(utility): 修复月份时间戳计算中的时区处理问题
修正 GetStartTimestampOfMonth、GetEndTimestampOfMonth 等月份相关时间戳计算方法,统一使用 CurrentTimeZone 计算 UTC 偏移量,确保时间戳转换的正确性。同时更新相关方法的 XML 注释以反映时区处理逻辑。
1 parent e66dfc0 commit f668c15

1 file changed

Lines changed: 32 additions & 18 deletions

File tree

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ public static DateTime GetStartTimeOfMonth(DateTime date)
5656
/// <remarks>
5757
/// 此方法返回指定日期所在月份的1号零点时间的Unix时间戳
5858
/// 例如:输入2024-01-10,返回2024-01-01 00:00:00的时间戳
59-
/// 会将时间转换为UTC时间后再计算时间戳
59+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
6060
/// </remarks>
6161
public static long GetStartTimestampOfMonth(DateTime date)
6262
{
63-
return new DateTimeOffset(GetStartTimeOfMonth(date)).ToUnixTimeSeconds();
63+
var time = GetStartTimeOfMonth(date);
64+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
6465
}
6566

6667
/// <summary>
@@ -87,11 +88,12 @@ public static DateTime GetEndTimeOfMonth(DateTime date)
8788
/// <remarks>
8889
/// 此方法返回指定日期所在月份的最后一天最后一秒的Unix时间戳
8990
/// 例如:输入2024-01-10,返回2024-01-31 23:59:59的时间戳
90-
/// 会将时间转换为UTC时间后再计算时间戳
91+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
9192
/// </remarks>
9293
public static long GetEndTimestampOfMonth(DateTime date)
9394
{
94-
return new DateTimeOffset(GetEndTimeOfMonth(date)).ToUnixTimeSeconds();
95+
var time = GetEndTimeOfMonth(date);
96+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
9597
}
9698

9799
/// <summary>
@@ -101,11 +103,12 @@ public static long GetEndTimestampOfMonth(DateTime date)
101103
/// <remarks>
102104
/// 此方法返回下个月1号零点时间的Unix时间戳
103105
/// 例如:当前是2024-01-10,返回2024-02-01 00:00:00的时间戳
104-
/// 会将时间转换为UTC时间后再计算时间戳
106+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
105107
/// </remarks>
106108
public static long GetNextMonthStartTimestamp()
107109
{
108-
return new DateTimeOffset(GetNextMonthStartTime()).ToUnixTimeSeconds();
110+
var time = GetNextMonthStartTime();
111+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
109112
}
110113

111114
/// <summary>
@@ -130,11 +133,12 @@ public static DateTime GetNextMonthEndTime()
130133
/// <remarks>
131134
/// 此方法返回下个月最后一天最后一秒的Unix时间戳
132135
/// 例如:当前是2024-01-10,返回2024-02-29 23:59:59的时间戳
133-
/// 会将时间转换为UTC时间后再计算时间戳
136+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
134137
/// </remarks>
135138
public static long GetNextMonthEndTimestamp()
136139
{
137-
return new DateTimeOffset(GetNextMonthEndTime()).ToUnixTimeSeconds();
140+
var time = GetNextMonthEndTime();
141+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
138142
}
139143

140144
/// <summary>
@@ -156,32 +160,37 @@ public static DateTime GetNextMonthStartTime()
156160
/// </summary>
157161
/// <returns>本月1号零点时间</returns>
158162
/// <remarks>
159-
/// 此方法基于UTC时间计算本月开始时间:
160-
/// 1. 获取当前UTC时间的年份和月份
163+
/// 此方法基于当前时区 (<see cref="CurrentTimeZone"/>) 时间计算本月开始时间:
164+
/// 1. 获取当前时区时间的年份和月份
161165
/// 2. 创建一个新的DateTime对象,设置为本月1号零点
162-
/// 3. 返回的时间为UTC时区的时间
166+
/// 3. 返回的时间为当前时区的时间
163167
///
164168
/// 示例:
165-
/// - 当前UTC时间为2024-01-15 14:30:00
166-
/// - 返回时间为2024-01-01 00:00:00 (UTC)
169+
/// - 当前时间为2024-01-15 14:30:00
170+
/// - 返回时间为2024-01-01 00:00:00
167171
///
168172
/// 注意:
169-
/// - 返回的是UTC时区的时间,如需本地时间请使用TimeZoneInfo.ConvertTimeFromUtc转换
173+
/// - 返回的是当前时区的时间
170174
/// - 返回时间的Hour/Minute/Second/Millisecond均为0
171175
/// </remarks>
172176
public static DateTime GetMonthStartTime()
173177
{
174-
var utcNow = GetUtcNow();
175-
return new DateTime(utcNow.Year, utcNow.Month, 1);
178+
var now = GetNow();
179+
return new DateTime(now.Year, now.Month, 1);
176180
}
177181

178182
/// <summary>
179183
/// 获取本月开始时间戳
180184
/// </summary>
181185
/// <returns>本月1号零点时间戳(秒)</returns>
186+
/// <remarks>
187+
/// 此方法返回本月1号零点时间的Unix时间戳
188+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
189+
/// </remarks>
182190
public static long GetMonthStartTimestamp()
183191
{
184-
return new DateTimeOffset(GetMonthStartTime()).ToUnixTimeSeconds();
192+
var time = GetMonthStartTime();
193+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
185194
}
186195

187196
/// <summary>
@@ -197,8 +206,13 @@ public static DateTime GetMonthEndTime()
197206
/// 获取本月结束时间戳
198207
/// </summary>
199208
/// <returns>本月最后一天23:59:59的时间戳(秒)</returns>
209+
/// <remarks>
210+
/// 此方法返回本月最后一天最后一秒的Unix时间戳
211+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
212+
/// </remarks>
200213
public static long GetMonthEndTimestamp()
201214
{
202-
return new DateTimeOffset(GetMonthEndTime()).ToUnixTimeSeconds();
215+
var time = GetMonthEndTime();
216+
return new DateTimeOffset(time, CurrentTimeZone.GetUtcOffset(time)).ToUnixTimeSeconds();
203217
}
204218
}

0 commit comments

Comments
 (0)