文章出處

在項目中我需要使用到一個隨機數(Random Number),該隨機數將作為 Hashtable 中的 Key 用于唯一索引數據,所以需要保持單機唯一性。

同時該隨機數還需要具備可排序性以便對數據進行排序。

此時,我可以考慮使用隨機字符串《C#生成MongoDB中的ObjectId》,這里的 ObjectId 具備單機唯一性和可排序性。

但另一個需求是,我需要該隨機數為 int 或 long 類型,以便與依賴的 Service 進行通信,部分取代 TransactionId 或 Token 的職責。

所以,很自然的我就想到了時間。

查看了 MSDN 上關于 DateTime.MinValueDateTime.Ticks 的定義,考慮到我所設計的服務每秒不會產生多于 10000000 的數據,

我決定使用 DateTime.Ticks 作為隨機數的起始點,這樣就有了下面這段代碼。

 1   public static class UniqueId
 2   {
 3     private static long _sequence = DateTime.UtcNow.Ticks;
 4 
 5     public static long Generate()
 6     {
 7       return Interlocked.Increment(ref _sequence);
 8     }
 9 
10     public static long Sequence
11     {
12       get
13       {
14         return _sequence;
15       }
16     }
17   }

業務邏輯中會將 UniqueId 存儲到 SQL Server 數據庫中,使用 bigint 類型進行存儲。

1 CREATE TABLE [TableName](
2     [UniqueId] [bigint] NOT NULL,
3 );

這樣,在數據庫中,該 UniqueId 即可作為索引,也可進行排序。

現在問題來了,我需要將一個較老的服務升級到新的服務中,做 Date Migration。

老的服務中沒有 UniqueId 邏輯和數據,這就需要我在數據遷移時為每條表數據生成該 UniqueId。

顯然,SQL Server 不會幫我做這件事,我需要使用 SQL Script 來生成 UniqueId。

DateTime.Ticks 的定義是:

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

1 Tick 等于千萬分之一秒,或萬分之一毫秒。

The value represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue.

DateTime.Ticks 代表著從時間 "00:00:00.0000000, January 1, 0001" 至當前時間的 Tick 總數。

這樣,我們就可以根據其原理通過 SQL 來計算其值。

 1 DECLARE @current_datetime DATETIME;
 2 DECLARE @days_from_0001_to_1900 BIGINT;
 3 DECLARE @ticks_per_millisecond BIGINT;
 4 DECLARE @ticks_per_day BIGINT;
 5 DECLARE @millisecond_of_time BIGINT;
 6 DECLARE @ticks_of_days BIGINT;
 7 DECLARE @ticks_of_time BIGINT;
 8 DECLARE @ticks BIGINT;
 9 
10 SET @current_datetime = GETUTCDATE();
11 SET @days_from_0001_to_1900 = 693595;
12 SET @ticks_per_millisecond = 10000;
13 SET @ticks_per_day = 24 * 3600 * 1000 * @ticks_per_millisecond;
14 SET @millisecond_of_time = 
15     DATEPART(MILLISECOND, @current_datetime) 
16     + (DATEPART(SECOND, @current_datetime)) * 1000 
17     + (DATEPART(MINUTE, @current_datetime)) * 60000 
18     + (DATEPART(HOUR, @current_datetime)) * 3600000;
19 SET @ticks_of_days = CAST(@days_from_0001_to_1900 + DATEDIFF(DAY, 0, @current_datetime) AS BIGINT) * @ticks_per_day;
20 SET @ticks_of_time = @millisecond_of_time * @ticks_per_millisecond;
21 SET @ticks = @ticks_of_days + @ticks_of_time;
22 
23 SELECT @current_datetime, @ticks

這段 SQL 腳本的運行結果是:

我們通過下面這段 C# 代碼來驗證結果。

 1   class Program
 2   {
 3     static void Main(string[] args)
 4     {
 5       DateTime d1 = new DateTime(2014, 04, 01, 02, 52, 54, 153, DateTimeKind.Utc);
 6       DateTime d2 = DateTime.Parse("2014-04-01 02:52:54.153");
 7       DateTime d3 = DateTime.Parse("2014-04-01 02:52:54.153",
 8         CultureInfo.CurrentCulture, DateTimeStyles.AdjustToUniversal);
 9 
10       Console.WriteLine(d1.Ticks);
11       Console.WriteLine(d2.Ticks);
12       Console.WriteLine(d3.Ticks);
13 
14       Console.ReadKey();
15     }
16   }

瞧,它們的結果一致為 635319175741530000。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()