欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入U(xiǎn)nix時(shí)間戳與C# DateTime時(shí)間類型互換的詳解

 更新時(shí)間:2013年06月05日 12:05:58   作者:  
本篇文章是對(duì)Unix時(shí)間戳與C# DateTime時(shí)間類型互換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
Unix時(shí)間戳最小單位是秒,開(kāi)始時(shí)間為格林威治標(biāo)準(zhǔn)時(shí)間1970-01-01 00:00:00
ConvertIntDateTime方法的基本思路是通過(guò)獲取本地時(shí)區(qū)表示Unixk開(kāi)始時(shí)間,加上Unix時(shí)間值(即過(guò)去的秒數(shù)).
ConvertDateTimeInt方法的基本思路是通過(guò)刻度數(shù)差,再把刻度數(shù)轉(zhuǎn)換為秒數(shù),當(dāng)然要說(shuō)明的是,我這里返回的是double類型,意義上并非是真正的Unix時(shí)間戳格式。
要獲取真正Unix時(shí)間戳的,只獲取整數(shù)部分就可以了。
復(fù)制代碼 代碼如下:

dangranusing System;
using System.Collections.Generic;
using System.Text;
namespace WWFramework.DateTimes
{
    /// <summary>
    /// 時(shí)間相關(guān)函數(shù)
    /// </summary>
    public static class Function
    {
        /// <summary>
        /// 將Unix時(shí)間戳轉(zhuǎn)換為DateTime類型時(shí)間
        /// </summary>
        /// <param name="d">double 型數(shù)字</param>
        /// <returns>DateTime</returns>
        public static System.DateTime ConvertIntDateTime(double d)
        {
            System.DateTime time = System.DateTime.MinValue;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            time = startTime.AddSeconds(d);
            return time;
        }
        /// <summary>
        /// 將c# DateTime時(shí)間格式轉(zhuǎn)換為Unix時(shí)間戳格式
        /// </summary>
        /// <param name="time">時(shí)間</param>
        /// <returns>double</returns>
        public static double ConvertDateTimeInt(System.DateTime time)
        {
            double intResult = 0;
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
            intResult = (time - startTime).TotalSeconds;
            return intResult;
        }
    }
}

相關(guān)文章

最新評(píng)論