我想要一个在一天(24 小时)内保持唯一的号码。以下是我想出的代码;我想知道它的谬误/可能的风险;“我相信”这保证了至少一天的 12 位唯一编号。
逻辑是获取当前日期/时间(hhmmssmmm)并连接查询性能计数器结果的前四个字节。
__forceinline bool GetUniqueID(char caUID[MAX_STRING_LENGTH])
{
//Logic: Add HHMMSSmmm with mid 3 bytes of performance counter.
//Guarantees that in a single milli second band (0 to 999) the three bytes
//of performance counter would always be unique.
//1. Get system time, and use
bool bStatus = false;
try
{
SYSTEMTIME localtime;
GetLocalTime(&localtime);//Get local time, so that we may pull out HHMMSSmmm
LARGE_INTEGER li;
char cNT[MAX_STRING_LENGTH];//new time.
memset(cNT, '\0', sizeof(cNT));
try
{
//Try to get the performance counter,
//if one is provided by the OEM.
QueryPerformanceCounter(&li);//This function retrieves the current value of the
//high-resolution performance counter if one is provided by the OEM
//We use the first four bytes only of it.
sprintf(cNT, "%u", li.QuadPart);
}
catch(...)
{
//Not provided by OEM.
//Lets go with the GetTickCounts();
//ddHHMMSS + 4 bytes of dwTicks
sprintf(cNT,"%04d", GetTickCount());
}
//Get the first four bytes.
int iSkipTo = 0;//This is incase we'd decide to pull out next four bytes, rather than first four bytes.
int iGetChars = 4;//Number of chars to get.
char *pSub = (char*) malloc(iGetChars+1);//Clear memory
strncpy(pSub, cNT + iSkipTo, iGetChars);//Get string
pSub[iGetChars] = '\0'; //Mark end.
//Prepare unique id
sprintf(caUID, "%02d%02d%02d%3d%s",
localtime.wHour,
localtime.wMinute,
localtime.wSecond,
localtime.wMilliseconds,
pSub); //First four characters concat.
bStatus = true;
}
catch(...)
{
//Couldnt prepare. There was some problem.
bStatus = false;
}
return bStatus;
}
以下是我得到的输出:
唯一:[125907 462224] 唯一:[125907 462225] 唯一:[125907 462226] 唯一:[125907 462227] 唯一:[125907 462228] 唯一:[125907 462230] 唯一:[125907 4627431] 唯一:[23] 唯一:[12590] [125907 462233] 唯一:[125907 462234] 唯一:[125907 462235] 唯一:[125907 462237] 唯一:[125907 462238] 唯一:[125907 462239] 唯一:[125907 462240] 唯一:[1259125] 唯一:[1259125] 462243] 唯一:[125907 462244] 唯一:[125907 462245] 唯一:[125907 462246] 唯一:[125907 462247] 唯一:[125907 462248] 唯一:[125907 462249] 唯一:[1225907 460272562]唯一:[125907 462253] 唯一:[125907 462254] 唯一:[125907 462255] 唯一:[125907 462256] 唯一:[125907 462257] 唯一:[125907 462258] 毫秒改变,46 唯一:[125907 62027261] 622262] 唯一:[125907 622263] 唯一:[125907 622264] 唯一:[125907 622265] 唯一:[125907 622267] 唯一:[125907 622268] 唯一:[125907 622269] 唯一:[125907 622270] 唯一:[125907 622271] 唯一:[125907 622273] 唯一:[125907 622274] 唯一:[1259125]6275] 622276] 唯一:[125907 622277] 唯一:[125907 622278] 唯一:[125907 622279] 唯一:[125907 622281] 唯一:[125907 622282] 唯一:[125907 622283] 唯一:[1225907 62027264]独特:[125907 622286] 独特:[125907 622288] 独特:[125907 622289] 独特:[125907 622290] 独特:[125907 622291] 独特:[125907 622292] 独特:[125907 6227293] 独特:[12590] 独特:[12590] [125907 622296] 唯一:[125907 622297] 唯一:[125907 622298] 唯一:[125907 622299] 唯一:[125907 622300] 唯一:[125907 622301] 唯一:[125907 622302] 唯一:[1259125] 唯一:[125912] 622305] 唯一:[125907 622306] 毫秒更改,62 唯一:[125907 782308] 唯一:[125907 782310] 唯一:[125907 782311] 唯一:[125907 782312] 唯一:[125907 782313] 唯一:[125907 782314] 唯一:[125907 782316] 唯一:[125907 782317] 唯一:[1259125] 唯一:[125912] 782319]毫秒改变,125个独特:[1259071402495]唯一:[1259071402497]唯一:[1259071402498]唯一:[1259071402499]独特:[1259071402500]独特:[1259071402502]独特:[1259071402503]独特:[1259071402504]独特:[1259071402504 ] 唯一:[1259071402507][1259071402504] 唯一:[1259071402505] 唯一:[1259071402507][1259071402504] 唯一:[1259071402505] 唯一:[1259071402507]
现在我正在考虑将生成的 ID 保存在一个列表中,并将新生成的 ID 与列表中的现有 ID 进行比较。如果它已经存在于列表中,那么我当然可以跳过这个数字并生成另一个,但当然很明显这个逻辑会失败。
非常感谢您的意见/建议/更新/等。
谢谢JT。