1 日 (24 時間) 固有のままになる番号が必要でした。以下は私が思いついたコードです。その誤謬/考えられるリスクについて疑問に思っていました。これにより、少なくとも 1 日は 12 桁の一意の番号が保証されると「信じています」。
ロジックは、現在の日付/時刻 (hhmmssmmm) を取得し、クエリ パフォーマンス カウンターの結果の最初の 4 バイトを連結することです。
__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 462231] ユニーク7:[12623] [125907 462233] ユニーク:[125907 462234] ユニーク:[125907 462235] ユニーク:[125907 462237] ユニーク:[125907 462238] ユニーク:[125907 462239] ユニーク:[125907 462240] ユニーク:[125907 90215] 462243] ユニーク:[125907 462244] ユニーク:[125907 462245] ユニーク:[125907 462246] ユニーク:[125907 462247] ユニーク:[125907 462248] ユニーク:[125907 462249] ユニーク:[125907 462255]ユニーク:[125907 462253] ユニーク:[125907 462254] ユニーク:[125907 462255] ユニーク:[125907 462256] ユニーク:[125907 462257] ユニーク:[125907 462258] ミリ秒変更、46 ユニーク:[125907 622215]9 622262] ユニーク:[125907 622263] ユニーク:[125907 622264] ユニーク:[125907 622265] ユニーク:[125907 622267] 固有:[125907 622268] 固有:[125907 622269] 固有:[125907 622270] 固有:[125907 622271] 固有:[125907 622273] 固有:[125907 622274] 固有:[125907 622274] 固有:[125907] 6ユニーク:[125907 622276] ユニーク:[125907 622278] ユニーク:[125907 622279] ユニーク:[125907 622281] ユニーク:[125907 622282] ユニーク:[125907 622283] ユニーク:[125907 622284]ユニーク:[125907 622286] ユニーク:[125907 622288] ユニーク:[125907 622289] ユニーク:[125907 622290] ユニーク:[125907 622291] ユニーク:[125907 622292] ユニーク:[125907 622293] ユニーク7:[122595] [125907 622296] ユニーク:[125907 622297] ユニーク:[125907 622298] ユニーク:[125907 622299] ユニーク:[125907 622300] ユニーク:[125907 622301] ユニーク:[125907 622302] ユニーク:[125907 622307] 622305] 固有:[125907 622306] ミリ秒変更、62 固有:[125907 782308] 固有:[125907 782310] ユニーク:[125907 782311] ユニーク:[125907 782312] ユニーク:[125907 782313] ユニーク:[125907 782314] ユニーク:[125907 782316] ユニーク:[125907 782317] ユニーク:[125907 782317] 782319] Millisecond changed, 125 Unique:[1259071402495] Unique:[1259071402497] Unique:[1259071402498] Unique:[1259071402499] Unique:[1259071402500] Unique:[1259071402502] Unique:[1259071402503] Unique:[1259071402504] Unique:[1259071402505 ] ユニーク:[1259071402507][1259071402504] ユニーク:[1259071402505] ユニーク:[1259071402507][1259071402504] ユニーク:[1259071402505] ユニーク:[1259071402507]
現在、生成された ID をリストに保持し、新しく生成された ID をリスト内の既存のものと比較することを考えています。リストに既に存在する場合は、確かに番号をスキップして別の番号を生成できますが、確かにそして明らかにこのロジックは失敗します。
コメント/提案/更新/などをいただければ幸いです。
ありがとうJT。