/// <summary>
/// 获取从格林威治时间到当前某一时刻的总毫秒数
/// </summary>
/// <param name="dateTime">北京时间</param>
/// <param name="accurateToMilliseconds">精确到毫秒,否到秒</param>
/// <returns>返回一个长整数时间戳</returns>
public static long GetUnixTimeStamp(DateTime dateTime, bool accurateToMilliseconds)
{
DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0));//北京所在东八区
DateTime endTime = TimeZoneInfo.ConvertTimeToUtc(dateTime);
return (long)(accurateToMilliseconds ? (endTime - startTime).TotalMilliseconds : (endTime - startTime).TotalSeconds);
}
/// <summary>
/// 将UNIX时间戳转为北京时间
/// </summary>
/// <param name="unixTimeStamp">时间戳</param>
/// <param name="accurateToMilliseconds">精确到毫秒,佛为秒</param>
/// <returns></returns>
public static DateTime GetLocalTime(long unixTimeStamp, bool accurateToMilliseconds)
{
DateTime startTime = TimeZoneInfo.ConvertTimeToUtc(new DateTime(1970, 1, 1, 8, 0, 0));//北京所在东八区
return (accurateToMilliseconds ? startTime.AddMilliseconds(unixTimeStamp) : startTime.AddSeconds(unixTimeStamp)).ToLocalTime();
}