You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
1.1 KiB
30 lines
1.1 KiB
using System; |
|
using System.Threading; |
|
|
|
public static class GUID |
|
{ |
|
private static ulong TimestampMask = 0xffffffff00000000; //时间戳掩码 |
|
private static ulong BigMask = 0x00000000f0000000; //大区号掩码 |
|
private static ulong SmallMask = 0x000000000ff00000; //小区号掩码 |
|
private static ulong BaseMask = 0x00000000000fffff; //本地 ID 编号掩码 |
|
private static int BaseId = 0; //本地 ID |
|
|
|
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); |
|
|
|
/// <summary> |
|
/// 根据大区号和小区号生成 ID。 |
|
/// </summary> |
|
public static ulong NewGuid(uint big = 0, uint small = 0) |
|
{ |
|
ulong timeStamp = (ulong)(DateTime.UtcNow - Jan1st1970).TotalSeconds; |
|
|
|
ulong newId = ((timeStamp << 32) & TimestampMask) | |
|
((big << 28) & BigMask) | |
|
((small << 20) & SmallMask) | |
|
((uint)BaseId & BaseMask); |
|
|
|
Interlocked.Increment(ref BaseId); |
|
|
|
return newId; |
|
} |
|
}
|
|
|