Browse Source

纳秒级时间戳实现

懒得勤快 6 years ago
parent
commit
264d29b18e

+ 17 - 0
Masuit.Tools.Core/DateTimeExt/DateUtil.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics;
 
 namespace Masuit.Tools.DateTimeExt
 {
@@ -8,6 +9,8 @@ namespace Masuit.Tools.DateTimeExt
     public static class DateUtil
     {
         private static readonly DateTime Start1970 = DateTime.Parse("1970-01-01 00:00:00");
+        private static readonly long Ticks1970 = Start1970.Ticks;
+
         /// <summary>
         /// 返回相对于当前时间的相对天数
         /// </summary>
@@ -62,6 +65,20 @@ namespace Masuit.Tools.DateTimeExt
         /// <returns></returns>
         public static double GetTotalMilliseconds(this DateTime dt) => (dt - Start1970).TotalMilliseconds;
 
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static long GetTotalMicroseconds(this DateTime dt) => (dt - Start1970).Ticks / 10;
+
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的纳秒时间戳
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static long GetTotalNanoseconds(this DateTime dt) => (dt - Start1970).Ticks * 100 + Stopwatch.GetTimestamp() % 100;
+
         /// <summary>
         /// 获取该时间相对于1970-01-01 00:00:00的分钟数
         /// </summary>

+ 4 - 25
Masuit.Tools.Core/Extensions.cs

@@ -1524,33 +1524,12 @@ namespace Masuit.Tools
         /// 生成唯一短字符串
         /// </summary>
         /// <param name="str"></param>
-        /// <param name="length">生成的字符串长度,越长冲突的概率越小,默认长度为6,最小长度为5,最大长度为22</param>
+        /// <param name="chars">可用字符数数量,0-9,a-z,A-Z</param>
         /// <returns></returns>
-        public static string CreateShortToken(this string str, int length = 6)
+        public static string CreateShortToken(this string str, byte chars = 36)
         {
-            var temp = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Trim('=');
-            if (length <= 22)
-            {
-                if (length < 5)
-                {
-                    length = 5;
-                }
-
-                temp = temp.Substring(0, length);
-            }
-
-            return Regex.Replace(temp, @"\p{P}|\+", string.Empty);
-        }
-
-        /// <summary>
-        /// 生成唯一短字符串
-        /// </summary>
-        /// <param name="str"></param>
-        /// <returns></returns>
-        public static string CreateShortToken(this string str)
-        {
-            var nf = new NumberFormater(36);
-            return nf.ToString(Stopwatch.GetTimestamp());
+            var nf = new NumberFormater(chars);
+            return nf.ToString((DateTime.Now.Ticks - 630822816000000000) * 100 + Stopwatch.GetTimestamp() % 100);
         }
 
         /// <summary>

+ 16 - 0
Masuit.Tools/DateTimeExt/DateUtil.cs

@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics;
 
 namespace Masuit.Tools.DateTimeExt
 {
@@ -8,6 +9,7 @@ namespace Masuit.Tools.DateTimeExt
     public static class DateUtil
     {
         private static readonly DateTime Start1970 = DateTime.Parse("1970-01-01 00:00:00");
+
         /// <summary>
         /// 返回相对于当前时间的相对天数
         /// </summary>
@@ -62,6 +64,20 @@ namespace Masuit.Tools.DateTimeExt
         /// <returns></returns>
         public static double GetTotalMilliseconds(this DateTime dt) => (dt - Start1970).TotalMilliseconds;
 
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的微秒时间戳
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static long GetTotalMicroseconds(this DateTime dt) => (dt - Start1970).Ticks / 10;
+
+        /// <summary>
+        /// 获取该时间相对于1970-01-01 00:00:00的纳秒时间戳
+        /// </summary>
+        /// <param name="dt"></param>
+        /// <returns></returns>
+        public static long GetTotalNanoseconds(this DateTime dt) => (dt - Start1970).Ticks * 100 + Stopwatch.GetTimestamp() % 100;
+
         /// <summary>
         /// 获取该时间相对于1970-01-01 00:00:00的分钟数
         /// </summary>

+ 4 - 25
Masuit.Tools/Extensions.cs

@@ -1523,33 +1523,12 @@ namespace Masuit.Tools
         /// 生成唯一短字符串
         /// </summary>
         /// <param name="str"></param>
-        /// <param name="length">生成的字符串长度,越长冲突的概率越小,默认长度为6,最小长度为5,最大长度为22</param>
+        /// <param name="chars">可用字符数数量,0-9,a-z,A-Z</param>
         /// <returns></returns>
-        public static string CreateShortToken(this string str, int length)
+        public static string CreateShortToken(this string str, byte chars = 36)
         {
-            var temp = Convert.ToBase64String(Guid.NewGuid().ToByteArray()).Trim('=');
-            if (length <= 22)
-            {
-                if (length < 5)
-                {
-                    length = 5;
-                }
-
-                temp = temp.Substring(0, length);
-            }
-
-            return Regex.Replace(temp, @"\p{P}|\+", string.Empty);
-        }
-
-        /// <summary>
-        /// 生成唯一短字符串
-        /// </summary>
-        /// <param name="str"></param>
-        /// <returns></returns>
-        public static string CreateShortToken(this string str)
-        {
-            var nf = new NumberFormater(36);
-            return nf.ToString(Stopwatch.GetTimestamp());
+            var nf = new NumberFormater(chars);
+            return nf.ToString((DateTime.Now.Ticks - 630822816000000000) * 100 + Stopwatch.GetTimestamp() % 100);
         }
 
         /// <summary>

+ 8 - 2
Test/Program.cs

@@ -1,10 +1,16 @@
-namespace Test
+using Masuit.Tools.DateTimeExt;
+using System;
+
+namespace Test
 {
     static class Program
     {
         static void Main(string[] args)
         {
-
+            DateTime time = DateTime.Now;
+            Console.WriteLine(time.GetTotalMilliseconds());
+            Console.WriteLine(time.GetTotalMicroseconds());
+            Console.WriteLine(time.GetTotalNanoseconds());
         }
     }
 }