|
1 | | -namespace GameFrameX.Utility; |
| 1 | +namespace GameFrameX.Utility; |
2 | 2 |
|
3 | 3 | /// <summary> |
4 | 4 | /// 时间帮助工具类 |
@@ -158,47 +158,69 @@ public static long TimeToSecond(DateTime time, bool utc = false) |
158 | 158 | /// </summary> |
159 | 159 | /// <param name="timestampSeconds">Unix时间戳,从1970年1月1日以来的秒数。</param> |
160 | 160 | /// <returns>自公元1年1月1日以来的刻度数。</returns> |
| 161 | + /// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception> |
161 | 162 | public static long TimestampToTicks(long timestampSeconds) |
162 | 163 | { |
| 164 | + if (timestampSeconds < -62135596800L || timestampSeconds > 253402300799L) |
| 165 | + { |
| 166 | + throw new ArgumentOutOfRangeException(nameof(timestampSeconds), "Timestamp is out of valid range for DateTime conversion."); |
| 167 | + } |
| 168 | + |
163 | 169 | // 将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; |
166 | 172 | } |
167 | 173 |
|
168 | 174 | /// <summary> |
169 | 175 | /// 将Unix毫秒时间戳转换为自公元1年1月1日以来的刻度数。 |
170 | 176 | /// </summary> |
171 | 177 | /// <param name="timestampMillisSeconds">Unix毫秒时间戳,从1970年1月1日以来的毫秒数。</param> |
172 | 178 | /// <returns>自公元1年1月1日以来的刻度数。</returns> |
| 179 | + /// <exception cref="ArgumentOutOfRangeException">当时间戳超出有效范围时抛出此异常</exception> |
173 | 180 | public static long TimestampMillisToTicks(long timestampMillisSeconds) |
174 | 181 | { |
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; |
178 | 190 | } |
179 | 191 |
|
180 | 192 | /// <summary> |
181 | | - /// 将给定的时间戳转换为相对于当前时间的 TimeSpan 对象。 |
| 193 | + /// 将给定的时间戳转换为相对于EpochUtc的 TimeSpan 对象。 |
182 | 194 | /// </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> |
185 | 198 | public static TimeSpan TimeSpanWithTimestamp(long timestamp) |
186 | 199 | { |
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); |
190 | 207 | } |
191 | 208 |
|
192 | 209 | /// <summary> |
193 | | - /// 将给定的时间戳转换为相对于当前本地时间的 TimeSpan 对象。 |
| 210 | + /// 将给定的时间戳转换为相对于EpochLocal的 TimeSpan 对象。 |
194 | 211 | /// </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> |
197 | 215 | public static TimeSpan TimeSpanLocalWithTimestamp(long timestamp) |
198 | 216 | { |
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); |
202 | 224 | } |
203 | 225 |
|
204 | 226 | #region 时间差计算函数 |
|
0 commit comments