Skip to content

Commit 2bdf1f8

Browse files
committed
fix(utility): 修复时间戳转换时区处理错误
修复 MillisecondsTimeStampToDateTime 方法中非UTC时间未正确转换为当前时区的问题 更新相关方法的注释以明确使用 CurrentTimeZone 而非系统本地时区
1 parent f668c15 commit 2bdf1f8

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static DateTime GetStartTimeOfDay(DateTime date)
133133
/// <remarks>
134134
/// 此方法返回指定日期零点时间的Unix时间戳
135135
/// 例如:输入2024-01-10 14:30:00,返回2024-01-10 00:00:00的时间戳
136-
/// 会将时间转换为UTC时间后再计算时间戳
136+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
137137
/// </remarks>
138138
public static long GetStartTimestampOfDay(DateTime date)
139139
{
@@ -178,7 +178,7 @@ public static DateTime GetEndTimeOfDay(DateTime date)
178178
/// <remarks>
179179
/// 此方法返回指定日期最后一秒的Unix时间戳
180180
/// 例如:输入2024-01-10 14:30:00,返回2024-01-10 23:59:59的时间戳
181-
/// 会将时间转换为UTC时间后再计算时间戳
181+
/// 会使用当前时区 (<see cref="CurrentTimeZone"/>) 计算偏移量并将时间转换为UTC时间后再计算时间戳
182182
/// </remarks>
183183
public static long GetEndTimestampOfDay(DateTime date)
184184
{
@@ -260,15 +260,15 @@ public static long GetTomorrowEndTimestamp()
260260

261261

262262
/// <summary>
263-
/// 按照本地时间判断两个时间戳是否是同一天
263+
/// 按照当前时区 (<see cref="CurrentTimeZone"/>) 时间判断两个时间戳是否是同一天
264264
/// </summary>
265265
/// <param name="timestamp1">时间戳1(Unix秒级时间戳)。例如:1704857400</param>
266266
/// <param name="timestamp2">时间戳2(Unix秒级时间戳)。例如:1704859200</param>
267-
/// <returns>如果两个时间戳转换为本地时间后是同一天,则返回true;否则返回false</returns>
267+
/// <returns>如果两个时间戳转换为当前时区 (<see cref="CurrentTimeZone"/>) 时间后是同一天,则返回true;否则返回false</returns>
268268
/// <remarks>
269-
/// 此方法会先将UTC时间戳转换为本地时间,然后比较是否为同一天
269+
/// 此方法会先将UTC时间戳转换为当前时区 (<see cref="CurrentTimeZone"/>) 时间,然后比较是否为同一天
270270
/// 比较时只考虑年月日,不考虑具体时间
271-
/// 使用系统默认时区进行UTC到本地时间的转换
271+
/// 使用当前时区 (<see cref="CurrentTimeZone"/>) 进行UTC到当前时区时间的转换
272272
/// </remarks>
273273
public static bool IsLocalSameDay(long timestamp1, long timestamp2)
274274
{
@@ -395,7 +395,7 @@ public static int GetCrossDays(DateTime startTime, int hour = 0)
395395
}
396396

397397
/// <summary>
398-
/// 获取从指定日期到当前本地日期之间跨越的天数
398+
/// 获取从指定日期到当前时区 (<see cref="CurrentTimeZone"/>) 日期之间跨越的天数
399399
/// </summary>
400400
/// <param name="startTime">起始日期。</param>
401401
/// <param name="hour">小时。</param>
@@ -455,13 +455,13 @@ public static int GetCrossDays(DateTime startTime, DateTime endTime, int hour =
455455
}
456456

457457
/// <summary>
458-
/// 获取两个本地时间戳之间的间隔天数
458+
/// 获取两个当前时区 (<see cref="CurrentTimeZone"/>) 时间戳之间的间隔天数
459459
/// </summary>
460-
/// <param name="startTimestamp">开始时间戳(秒),UTC时间戳将被转换为本地时间</param>
461-
/// <param name="endTimestamp">结束时间戳(秒),UTC时间戳将被转换为本地时间</param>
460+
/// <param name="startTimestamp">开始时间戳(秒),UTC时间戳将被转换为当前时区 (<see cref="CurrentTimeZone"/>) 时间</param>
461+
/// <param name="endTimestamp">结束时间戳(秒),UTC时间戳将被转换为当前时区 (<see cref="CurrentTimeZone"/>) 时间</param>
462462
/// <returns>间隔天数,如果开始时间晚于结束时间,返回负数</returns>
463463
/// <remarks>
464-
/// 此方法会先将UTC时间戳转换为本地时间,然后计算两个本地时间之间的天数差
464+
/// 此方法会先将UTC时间戳转换为当前时区 (<see cref="CurrentTimeZone"/>) 时间,然后计算两个时间之间的天数差
465465
/// 计算时会考虑日期的时分秒部分
466466
/// </remarks>
467467
public static int GetCrossLocalDays(long startTimestamp, long endTimestamp)
@@ -473,15 +473,15 @@ public static int GetCrossLocalDays(long startTimestamp, long endTimestamp)
473473

474474

475475
/// <summary>
476-
/// 获取当前本地时区的日期,格式为yyyyMMdd的整数
476+
/// 获取当前时区 (<see cref="CurrentTimeZone"/>) 的日期,格式为yyyyMMdd的整数
477477
/// </summary>
478-
/// <returns>返回一个8位整数,表示当前本地时区的日期。例如:20231225表示2023年12月25日</returns>
478+
/// <returns>返回一个8位整数,表示当前时区 (<see cref="CurrentTimeZone"/>) 的日期。例如:20231225表示2023年12月25日</returns>
479479
/// <remarks>
480-
/// 此方法将当前本地时间转换为8位数字格式:
480+
/// 此方法将当前时区 (<see cref="CurrentTimeZone"/>) 时间转换为8位数字格式:
481481
/// - 前4位表示年份
482482
/// - 中间2位表示月份
483483
/// - 最后2位表示日期
484-
/// 使用DateTime.Now获取本地时间
484+
/// 使用 <see cref="GetNow"/> 获取当前时区时间
485485
/// </remarks>
486486
public static int CurrentDateWithDay()
487487
{

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,13 @@ public partial class TimerHelper
4444
/// <returns>转换后的时间。</returns>
4545
public static DateTime MillisecondsTimeStampToDateTime(long timestamp, bool utc = false)
4646
{
47+
var dateTime = EpochUtc.AddMilliseconds(timestamp);
4748
if (utc)
4849
{
49-
return EpochUtc.AddMilliseconds(timestamp);
50+
return dateTime;
5051
}
5152

53+
return TimeZoneInfo.ConvertTimeFromUtc(dateTime, CurrentTimeZone);
5254
return EpochLocal.AddMilliseconds(timestamp);
5355
}
5456

0 commit comments

Comments
 (0)