/// <summary> /// UTC时间戳转为时间 /// </summary> /// <param name="timeStamp">时间戳</param> /// <param name="accurate">时间戳精度(默认:秒)</param> /// <returns>返回一个日期时间</returns> public static DateTime TimeStampToTime(long timeStamp, EnumAccurateUTC accurate = EnumAccurateUTC.Second) { var startTime = TimeZoneInfo.ConvertTimeFromUtc(new System.DateTime(1970, 1, 1, 0, 0, 0, 0), TimeZoneInfo.Local); switch (accurate) { case EnumAccurateUTC.Second: return(startTime.AddTicks(timeStamp * 10000000)); case EnumAccurateUTC.Millisecond: return(startTime.AddTicks(timeStamp * 10000)); default: throw new Exception($"无法识别的UTC时间戳精度:{accurate}"); } }
/// <summary> /// 转UTC时间戳,单位(秒) /// </summary> /// <param name="clientTime">本地时间</param> /// <param name="accurate">时间戳精度(默认:秒)</param> /// <returns></returns> public static long ToTimeStamp(DateTime clientTime, EnumAccurateUTC accurate = EnumAccurateUTC.Second) { var startTime = TimeZoneInfo.ConvertTimeFromUtc(new System.DateTime(1970, 1, 1, 0, 0, 0, 0), TimeZoneInfo.Local); switch (accurate) { case EnumAccurateUTC.Second: return(Convert.ToInt64((clientTime - startTime).TotalSeconds)); case EnumAccurateUTC.Millisecond: return(Convert.ToInt64((clientTime - startTime).TotalMilliseconds)); default: throw new Exception($"无法识别的UTC时间戳精度:{accurate}"); } }