Skip to content

Commit 805bd9e

Browse files
committed
feat(utility): 添加时区支持到 TimerHelper
新增 CurrentTimeZone 静态属性及 SetTimeZone 方法,允许获取和设置全局时区。同时更新 EpochLocal 的注释,明确其不受自定义时区影响。这为跨时区的时间计算提供了基础支持。
1 parent 1afb7c3 commit 805bd9e

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

GameFrameX.Foundation.Utility/Time/TimerHelper.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,41 @@ namespace GameFrameX.Foundation.Utility;
3636
/// </summary>
3737
public partial class TimerHelper
3838
{
39+
/// <summary>
40+
/// 当前时区,默认为 UTC
41+
/// </summary>
42+
private static TimeZoneInfo _currentTimeZone = TimeZoneInfo.Utc;
43+
44+
/// <summary>
45+
/// 获取当前时区
46+
/// </summary>
47+
public static TimeZoneInfo CurrentTimeZone => _currentTimeZone;
48+
49+
/// <summary>
50+
/// 设置当前时区
51+
/// </summary>
52+
/// <param name="timeZone">时区信息</param>
53+
public static void SetTimeZone(TimeZoneInfo timeZone)
54+
{
55+
_currentTimeZone = timeZone ?? TimeZoneInfo.Utc;
56+
}
57+
58+
/// <summary>
59+
/// 设置当前时区
60+
/// </summary>
61+
/// <param name="timeZoneId">时区ID,如 "China Standard Time" 或 "UTC"</param>
62+
public static void SetTimeZone(string timeZoneId)
63+
{
64+
try
65+
{
66+
_currentTimeZone = TimeZoneInfo.FindSystemTimeZoneById(timeZoneId);
67+
}
68+
catch
69+
{
70+
_currentTimeZone = TimeZoneInfo.Utc;
71+
}
72+
}
73+
3974
/// <summary>
4075
/// Unix 纪元时间:1970-01-01 00:00:00 本地时间。
4176
/// </summary>
@@ -45,6 +80,8 @@ public partial class TimerHelper
4580
/// <remarks>
4681
/// 此常量用于本地时间与 Unix 时间戳之间的转换计算。
4782
/// Unix 纪元是计算机系统中时间戳的起始参考点。
83+
/// 注意:此字段始终基于系统本地时区,不随 <see cref="CurrentTimeZone"/> 变化。
84+
/// 如需基于当前设置时区的纪元时间,请使用 TimeZoneInfo.ConvertTime(EpochUtc, CurrentTimeZone)。
4885
/// </remarks>
4986
/// <seealso cref="EpochUtc"/>
5087
public static readonly DateTime EpochLocal = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Local);

0 commit comments

Comments
 (0)