12 位唯一 ID - 代码可靠性 [英] 12 Digit Unique ID - Code reliability

查看:40
本文介绍了12 位唯一 ID - 代码可靠性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个在一天(24 小时)内保持唯一的数字.以下是我想出的代码;我想知道它的谬误/可能的风险;我相信"这保证了至少一天的 12 位唯一编号.

I wanted a number that would remain unique for a day (24 hours). Following is the code that I came up with; I was wondering about its fallacies/possible risks; 'I believe' this guarantees a 12 digit unique number for a day atleast.

逻辑是获取当前日期/时间(hhmmssmmm)并连接查询性能计数器结果的前四个字节.

Logic is to get the current date/time (hhmmssmmm) and concat the first four bytes of query performance counter result.

__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]

Unique:[125907 462224] Unique:[125907 462225] Unique:[125907 462226] Unique:[125907 462227] Unique:[125907 462228] Unique:[125907 462230] Unique:[125907 462231] Unique:[125907 462232] Unique:[125907 462233] Unique:[125907 462234] Unique:[125907 462235] Unique:[125907 462237] Unique:[125907 462238] Unique:[125907 462239] Unique:[125907 462240] Unique:[125907 462241] Unique:[125907 462243] Unique:[125907 462244] Unique:[125907 462245] Unique:[125907 462246] Unique:[125907 462247] Unique:[125907 462248] Unique:[125907 462249] Unique:[125907 462251] Unique:[125907 462252] Unique:[125907 462253] Unique:[125907 462254] Unique:[125907 462255] Unique:[125907 462256] Unique:[125907 462257] Unique:[125907 462258] Millisecond changed, 46 Unique:[125907 622261] Unique:[125907 622262] Unique:[125907 622263] Unique:[125907 622264] Unique:[125907 622265] Unique:[125907 622267] Unique:[125907 622268] Unique:[125907 622269] Unique:[125907 622270] Unique:[125907 622271] Unique:[125907 622273] Unique:[125907 622274] Unique:[125907 622275] Unique:[125907 622276] Unique:[125907 622277] Unique:[125907 622278] Unique:[125907 622279] Unique:[125907 622281] Unique:[125907 622282] Unique:[125907 622283] Unique:[125907 622284] Unique:[125907 622285] Unique:[125907 622286] Unique:[125907 622288] Unique:[125907 622289] Unique:[125907 622290] Unique:[125907 622291] Unique:[125907 622292] Unique:[125907 622293] Unique:[125907 622295] Unique:[125907 622296] Unique:[125907 622297] Unique:[125907 622298] Unique:[125907 622299] Unique:[125907 622300] Unique:[125907 622301] Unique:[125907 622302] Unique:[125907 622304] Unique:[125907 622305] Unique:[125907 622306] Millisecond changed, 62 Unique:[125907 782308] Unique:[125907 782310] Unique:[125907 782311] Unique:[125907 782312] Unique:[125907 782313] Unique:[125907 782314] Unique:[125907 782316] Unique:[125907 782317] Unique:[125907 782318] Unique:[125907 782319] Millisecond changed, 125 Unique:[1259071402495] Unique:[1259071402497] Unique:[1259071402498] Unique:[1259071402499] Unique:[1259071402500] Unique:[1259071402502] Unique:[1259071402503] Unique:[1259071402504] Unique:[1259071402505] Unique:[1259071402507]

现在我想将生成的 ID 保存在一个列表中,并将新生成的 ID 与列表中的现有 ID 进行比较.如果它已经存在于列表中,那么我当然可以跳过这个数字并生成另一个,但显然这个逻辑会失败.

Now I am thinking of keeping the generated IDs in a list, and compare the new generated one with the existing ones in the list. If it already exist in the list, then well I can certainly skip the number and generate another, but certainly and clearly this logic would fail.

感谢您的评论/建议/更新/等.

Would appreciate your comments/suggestions/updates/etc.

谢谢JT.

推荐答案

我的解决方案是获取系统时间并为其添加一个计数器(伪代码):

My solution was to get the system time and add a counter to that (pseudo code):

static int counter = 0;
static Time lastTime;

String getNextId() {
    Time now = System.getTime();
    if (lastTime == now)
        counter ++;
    else
        counter = 0;
    return now+counter;
}

这将保证即使我调用该方法的频率高于 getTime() 更改时,我也会获得一个新 ID.

This would guarantee that I'd get a new ID even when I called the method more often than getTime() changes.

这篇关于12 位唯一 ID - 代码可靠性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆