Skip to content

Commit 2a8d858

Browse files
committed
[修改] 修改时间转换函数的计算错误
1 parent 355a0ef commit 2a8d858

1 file changed

Lines changed: 40 additions & 18 deletions

File tree

GameFrameX.Utility/TimeHelper.cs

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace GameFrameX.Utility;
1+
namespace GameFrameX.Utility;
22

33
/// <summary>
44
/// 时间帮助工具类
@@ -158,47 +158,69 @@ public static long TimeToSecond(DateTime time, bool utc = false)
158158
/// </summary>
159159
/// <param name="timestampSeconds">Unix时间戳,从1970年1月1日以来的秒数。</param>
160160
/// <returns>自公元1年1月1日以来的刻度数。</returns>
161+
/// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception>
161162
public static long TimestampToTicks(long timestampSeconds)
162163
{
164+
if (timestampSeconds < -62135596800L || timestampSeconds > 253402300799L)
165+
{
166+
throw new ArgumentOutOfRangeException(nameof(timestampSeconds), "Timestamp is out of valid range for DateTime conversion.");
167+
}
168+
163169
// 将Unix时间戳转换为刻度数,每秒等于10000000刻度
164-
// 621355968000000000是公元1年1月1日至1970年1月1日的刻度数差值
165-
return timestampSeconds * 10000000L + 621355968000000000L;
170+
// 使用TimeHelper.EpochUtc.Ticks确保与项目中其他时间计算保持一致
171+
return timestampSeconds * TimeSpan.TicksPerSecond + EpochUtc.Ticks;
166172
}
167173

168174
/// <summary>
169175
/// 将Unix毫秒时间戳转换为自公元1年1月1日以来的刻度数。
170176
/// </summary>
171177
/// <param name="timestampMillisSeconds">Unix毫秒时间戳,从1970年1月1日以来的毫秒数。</param>
172178
/// <returns>自公元1年1月1日以来的刻度数。</returns>
179+
/// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception>
173180
public static long TimestampMillisToTicks(long timestampMillisSeconds)
174181
{
175-
// 将Unix毫秒时间戳转换为刻度数,每毫秒等于10000000000刻度
176-
// 621355968000000000是公元1年1月1日至1970年1月1日的刻度数差值
177-
return timestampMillisSeconds * 10000L + 621355968000000000L;
182+
if (timestampMillisSeconds < -62135596800000L || timestampMillisSeconds > 253402300799999L)
183+
{
184+
throw new ArgumentOutOfRangeException(nameof(timestampMillisSeconds), "Timestamp is out of valid range for DateTime conversion.");
185+
}
186+
187+
// 将Unix毫秒时间戳转换为刻度数,每毫秒等于10000刻度
188+
// 使用TimeHelper.EpochUtc.Ticks确保与项目中其他时间计算保持一致
189+
return timestampMillisSeconds * TimeSpan.TicksPerMillisecond + EpochUtc.Ticks;
178190
}
179191

180192
/// <summary>
181-
/// 将给定的时间戳转换为相对于当前时间的 TimeSpan 对象。
193+
/// 将给定的时间戳转换为相对于EpochUtc的 TimeSpan 对象。
182194
/// </summary>
183-
/// <param name="timestamp">自某个固定时间点(通常为1970年1月1日午夜)以来经过的毫秒数。</param>
184-
/// <returns>一个 TimeSpan 对象,表示从给定时间戳到当前时间的间隔。</returns>
195+
/// <param name="timestamp">自1970年1月1日午夜以来经过的秒数。</param>
196+
/// <returns>一个 TimeSpan 对象,表示从EpochUtc到给定时间戳的间隔。</returns>
197+
/// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception>
185198
public static TimeSpan TimeSpanWithTimestamp(long timestamp)
186199
{
187-
// 计算当前时间与给定时间戳表示的时间之间的差值
188-
var timeSpan = MillisecondsTimeStampToDateTime(UnixTimeMilliseconds(), true) - MillisecondsTimeStampToDateTime(timestamp, true);
189-
return timeSpan;
200+
if (timestamp < -62135596800L || timestamp > 253402300799L)
201+
{
202+
throw new ArgumentOutOfRangeException(nameof(timestamp), "Timestamp is out of valid range for DateTime conversion.");
203+
}
204+
205+
// 直接将秒数转换为TimeSpan
206+
return TimeSpan.FromSeconds(timestamp);
190207
}
191208

192209
/// <summary>
193-
/// 将给定的时间戳转换为相对于当前本地时间的 TimeSpan 对象。
210+
/// 将给定的时间戳转换为相对于EpochLocal的 TimeSpan 对象。
194211
/// </summary>
195-
/// <param name="timestamp">自某个固定时间点(通常为1970年1月1日午夜)以来经过的毫秒数。</param>
196-
/// <returns>一个 TimeSpan 对象,表示从给定时间戳到当前本地时间的间隔。</returns>
212+
/// <param name="timestamp">自1970年1月1日午夜以来经过的秒数。</param>
213+
/// <returns>一个 TimeSpan 对象,表示从EpochLocal到给定时间戳的间隔。</returns>
214+
/// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception>
197215
public static TimeSpan TimeSpanLocalWithTimestamp(long timestamp)
198216
{
199-
// 计算当前时间与给定时间戳表示的时间之间的差值
200-
var timeSpan = DateTime.Now - MillisecondsTimeStampToDateTime(timestamp, true);
201-
return timeSpan;
217+
if (timestamp < -62135596800L || timestamp > 253402300799L)
218+
{
219+
throw new ArgumentOutOfRangeException(nameof(timestamp), "Timestamp is out of valid range for DateTime conversion.");
220+
}
221+
222+
// 直接将秒数转换为TimeSpan
223+
return TimeSpan.FromSeconds(timestamp);
202224
}
203225

204226
#region 时间差计算函数

0 commit comments

Comments
 (0)