【watcher】 #02 c# 中實現時間戳等,日期數字及大機率絕對隨機數 實現

在Wacher的項目中,用到了不少時間記錄的地方,爲了未來可以和在線數據打通,咱們使用了時間戳來記錄時間信息html

 

 

因爲c# 沒有現成的方法,因此咱們從新寫了一個Helper類來幫助咱們使用這些公共函數git

同時因爲是靜態函數,添加引用後咱們即可以全局調用了。github

 

 

一、經過日期獲取當前的時間戳算法

這個時間戳是10位的時間戳,若是須要和JAVA兼容請在除法中取出3位,保存到毫秒級c#

        /// <summary>
        /// 獲取時間戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeSecond(DateTime dataTime)
        {
            return ((dataTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString();
        }

 

二、經過時間戳獲取到DateTime信息api

(不管是string仍是long類型,方法中價格強制類型轉換便可,目前若是傳參是long類型的話只須要ToString便可 )網絡

        /// <summary>
        /// 由時間戳到系統時間
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime ReturnDateTime(string date)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(date + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return dtStart.Add(toNow);
        }

 

三、經過DateTime 獲取數字類型的日期時間dom

(經過DateTime便可得到 20180808 類型的數字日期)ide

        /// <summary>
        /// 經過datetime格式獲取 YMD格式數字類型日期
        /// 
        /// </summary>
        /// <param name="dataTime"></param>
        /// <returns></returns>
        public static long GetDateInt(DateTime dataTime)
        {
            var dateLong= dataTime.ToString("yyyyMMdd");
            return Convert.ToInt64(dateLong);
        }

 

四、獲取絕對隨機字符或絕對隨機函數

單機每秒請求10W次不重複,【在網絡請求中,咱們常常會用到request_id,服務端,客戶端都可使用,實測一秒內10W次請求不重複】

原理:

使用基於計算機自己的識別因子隨機數的隨機因子+基於GUid的隨機因子的隨機數+短期戳+base64進制轉化爲短字符。

 

參考資料:http://www.javashuo.com/article/p-uyzmeyrj-cz.html

              :https://blog.csdn.net/zmq5411/article/details/47322257 

 

/// <summary>
        /// 獲取絕對隨機數
        /// </summary>
        /// <returns></returns>
        public static string GetRandOnlyId()
        {
             var timeStamp= (DateTime.Now.ToUniversalTime().Ticks - 13560192000000000) / 10000000;// 減小時間戳位數造成新的短期戳
            var beginRand= IntToi64(new Random(GetRandomSeed()).Next(0, 99999999));// 基於計算機硬件的隨機因子產生隨機數
            var endRand= IntToi64(new Random(GetGuidSeed()).Next(0, 99999999));// 基於Guid隨機因子產生的的隨機數
            var randString = beginRand+ IntToi64(timeStamp)+ endRand;
            return randString;
        }

        /// <summary>
        /// 獲取不重複的隨機數種子
        /// system.Security.Cryptography.RNGCryptoServiceProvider的類,它採用系統當前的硬件信息、進程信息、線程信息、系統啓動時間和當前精確時間做爲填充因子,經過更好的算法生成高質量的隨機數
        /// </summary>
        /// <returns></returns>
        static int GetRandomSeed()
        {
            byte[] bytes = new byte[4];
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytes);
            return BitConverter.ToInt32(bytes, 0);

        }

        /// <summary>
        /// 經過Guid  獲取隨機種子
        /// </summary>
        /// <returns></returns>
        static int GetGuidSeed()
        {
            byte[] buffer = Guid.NewGuid().ToByteArray();
            int iSeed = BitConverter.ToInt32(buffer, 0);
            return iSeed;
        }

        /// <summary>
        /// 十進制轉64進制
        /// </summary>
        /// <param name="xx"></param>
        /// <returns></returns>
        public static string IntToi64(long xx)
        {
            string retStr = "";
            while (xx >= 1)
            {
                int index = Convert.ToInt16(xx - (xx / 64) * 64);
                retStr = Base64Code[index] + retStr;
                xx = xx / 64;
            }
            return retStr;
        }

        /// <summary>
        /// 64 位轉化參數
        /// </summary>
        private static Dictionary<int, string> Base64Code = new Dictionary<int, string>() {
            {   0  ,"A"}, {   1  ,"B"}, {   2  ,"C"}, {   3  ,"D"}, {   4  ,"E"}, {   5  ,"F"}, {   6  ,"G"}, {   7  ,"H"}, {   8  ,"I"}, {   9  ,"J"},
            {   10  ,"K"}, {   11  ,"L"}, {   12  ,"M"}, {   13  ,"N"}, {   14  ,"O"}, {   15  ,"P"}, {   16  ,"Q"}, {   17  ,"R"}, {   18  ,"S"}, {   19  ,"T"},
            {   20  ,"U"}, {   21  ,"V"}, {   22  ,"W"}, {   23  ,"X"}, {   24  ,"Y"}, {   25  ,"Z"}, {   26  ,"a"}, {   27  ,"b"}, {   28  ,"c"}, {   29  ,"d"},
            {   30  ,"e"}, {   31  ,"f"}, {   32  ,"g"}, {   33  ,"h"}, {   34  ,"i"}, {   35  ,"j"}, {   36  ,"k"}, {   37  ,"l"}, {   38  ,"m"}, {   39  ,"n"},
            {   40  ,"o"}, {   41  ,"p"}, {   42  ,"q"}, {   43  ,"r"}, {   44  ,"s"}, {   45  ,"t"}, {   46  ,"u"}, {   47  ,"v"}, {   48  ,"w"}, {   49  ,"x"},
            {   50  ,"y"}, {   51  ,"z"}, {   52  ,"0"}, {   53  ,"1"}, {   54  ,"2"}, {   55  ,"3"}, {   56  ,"4"}, {   57  ,"5"}, {   58  ,"6"}, {   59  ,"7"},
            {   60  ,"8"}, {   61  ,"9"}, {   62  ,"+"}, {   63  ,"/"}, };

 

 

最後持續更新的 watcher beta 版下載:http://api.bobdong.cn/public/static/Win/Watcher%E5%AE%88%E6%9C%9B%E8%80%85.exe

 

Github地址:https://github.com/d100000/Watcher

 

歡迎你們提出建議

相關文章
相關標籤/搜索