在Wacher的項目中,用到了很多時間記錄的地方,為了將來能夠和在線數據打通,我們使用了時間戳來記錄時間信息
由於c# 沒有現成的方法,所以我們重新寫了一個Helper類來幫助我們使用這些公共函數
同時由於是靜態函數,添加引用后我們便可以全局調用了。
1、通過日期獲取當前的時間戳
這個時間戳是10位的時間戳,如果需要和JAVA兼容請在除法中取出3位,保存到毫秒級
/// <summary> /// 獲取時間戳 /// </summary> /// <returns></returns> public static string GetTimeSecond(DateTime dataTime) { return ((dataTime.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString(); }
2、通過時間戳獲取到DateTime信息
(無論是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); }
3、通過DateTime 獲取數字類型的日期時間
(通過DateTime即可獲得 20180808 類型的數字日期)
/// <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); }
4、獲取絕對隨機字符或絕對隨機
單機每秒請求10W次不重復,【在網絡請求中,我們經常會用到request_id,服務端,客戶端均可使用,實測一秒內10W次請求不重復】
原理:
使用基於計算機本身的識別因子隨機數的隨機因子+基於GUid的隨機因子的隨機數+短時間戳+base64進制轉化為短字符。
參考資料:https://www.cnblogs.com/linJie1930906722/p/6115917.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
歡迎大家提出建議