Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:41:34+00:00 2026-05-11T16:41:34+00:00

I wanted a number that would remain unique for a day (24 hours). Following

  • 0

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.

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;
}

Following is the output that I get:

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]

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.

Thanks
JT.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:41:34+00:00Added an answer on May 11, 2026 at 4:41 pm

    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;
    }
    

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a quick question about cookie security that I wanted to run by
If I wanted to convert a 64 bit number that reprasents time in Windows
I wanted to count number of characters in a string which I pull from
I just wanted to compare a double number saved in a double variable in
Recently I've wanted to do a number of things using CSS only to find
I wanted to write a regex to count the number of spaces/tabs/newline in a
Out of curiosity I wanted to test the number of ticks to compare a
Wanted to know if someone had a suggestion on code or maybe there's a
I have a free application that has a number of paid-for upgrade applications you
I have a class that contains a static number of objects. This class needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.