【发布时间】:2010-10-15 09:44:51
【问题描述】:
我想要一个在一天(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 462231] 唯一:[125907 462232] 唯一:[125907 462233] 唯一:[125907 462234] 唯一:[125907 462235] 唯一:[125907 462237] 唯一:[125907 462238] 唯一:[125907 462239] 唯一:[125907 462240] 唯一:[125907 462241] 唯一:[125907 462243] 唯一:[125907 462244] 唯一:[125907 462245] 唯一:[125907 462246] 唯一:[125907 462247] 唯一:[125907 462248] 唯一:[125907 462249] 唯一:[125907 462251] 唯一:[125907 462252] 唯一:[125907 462253] 唯一:[125907 462254] 唯一:[125907 462255] 唯一:[125907 462256] 唯一:[125907 462257] 唯一:[125907 462258] 毫秒变化,46 唯一:[125907 622261] 唯一:[125907 622262] 唯一:[125907 622263] 唯一:[125907 622264] 唯一:[125907 622265] 唯一:[125907 622267] 唯一:[125907 622268] 唯一:[125907 622269] 唯一:[125907 622270] 唯一:[125907 622271] 唯一:[125907 622273] 唯一:[125907 622274] 唯一:[125907 622275] 唯一:[125907 622276] 唯一:[125907 622277] 唯一:[125907 622278] 唯一:[125907 622279] 唯一:[125907 622281] 唯一:[125907 622282] 唯一:[125907 622283] 唯一:[125907 622284] 唯一:[125907 622285] 唯一:[125907 622286] 唯一:[125907 622288] 唯一:[125907 622289] 唯一:[125907 622290] 唯一:[125907 622291] 唯一:[125907 622292] 唯一:[125907 622293] 唯一:[125907 622295] 唯一:[125907 622296] 唯一:[125907 622297] 唯一:[125907 622298] 唯一:[125907 622299] 唯一:[125907 622300] 唯一:[125907 622301] 唯一:[125907 622302] 唯一:[125907 622304] 唯一:[125907 622305] 唯一:[125907 622306] 毫秒变化,62 唯一:[125907 782308] 唯一:[125907 782310] 唯一:[125907 782311] 唯一:[125907 782312] 唯一:[125907 782313] 唯一:[125907 782314] 唯一:[125907 782316] 唯一:[125907 782317] 唯一:[125907 782318] 唯一:[125907 782319] 毫秒变化,125 唯一:[1259071402495] 唯一:[1259071402497] 唯一:[1259071402498] 唯一:[1259071402499] 唯一:[1259071402500] 唯一:[1259071402502] 唯一:[1259071402503] 唯一:[1259071402504] 唯一:[1259071402505] 唯一:[1259071402507]
现在我正在考虑将生成的 ID 保存在一个列表中,并将新生成的 ID 与列表中的现有 ID 进行比较。如果它已经存在于列表中,那么我当然可以跳过这个数字并生成另一个,但是这个逻辑肯定会失败。
非常感谢您的 cmets/建议/更新/等。
谢谢 JT。
【问题讨论】:
-
为什么 malloc psub 为 5 个字节,而不是可以用 psub[5]={0}; 进行初始化;
-
拉斯赫曼,感谢您的建议。感谢您的回复。
标签: c++ unique uniqueidentifier