12345678910111213141516171819202122232425 |
- using System;
- namespace TBL.CSharp.Base
- {
- public static partial class Extension
- {
- /// <summary>
- /// 从 Unix 时间戳转换为 <see cref="DateTime"/> 结构
- /// </summary>
- public static DateTime FromUnixTime(this long unixTime)
- {
- var epoch = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
- return epoch.AddSeconds(unixTime);
- }
- /// <summary>
- /// 从 <see cref="DateTime"/> 结构转换为 Unix 时间戳
- /// </summary>
- public static long ToUnixTime(this DateTime date)
- {
- var epoch = new DateTime(1970, 1, 1, 0, 0, 0, System.DateTimeKind.Utc);
- return Convert.ToInt64((date - epoch).TotalSeconds);
- }
- }
- }
|