在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
欢迎大家提出建议