C#中的UINX时间戳

2012-03-27  金城  6669

C#中

#region 将时间转换成UNIX时间戳[GetStamp(DateTime dt)]
/// <summary>
/// 将时间转换成UNIX时间戳
/// </summary>
/// <param name="dt">时间</param>
/// <returns>UNIX时间戳</returns>
public static UInt32 GetStamp(DateTime dt){
    TimeSpan ts = dt - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1));
    UInt32 uiStamp = Convert.ToUInt32(ts.TotalSeconds);
    return uiStamp;
}
#endregion

#region 将当前时间转换成UNIX时间戳[GetStamp()]
/// <summary>
/// 将当前时间转换成UNIX时间戳
/// </summary>
/// <returns>UNIX时间戳</returns>
public static UInt32 GetStamp(){
   return GetStamp(DateTime.Now);
}
#endregion

#region 将UNIX时间戳转换成时间[GetDateTime(UInt32 uiStamp)]
/// <summary>
/// 将UNIX时间戳转换成时间
/// </summary>
/// <param name="uiStamp">UNIX时间戳</param>
/// <returns>时间</returns>
public static DateTime GetDateTime(UInt32 uiStamp){
   DateTime dt = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970,1,1)).AddSeconds(uiStamp);
   return dt;
}
#endregion