How to use system time to generate unique id?

G

Guest

I need to generate unique id for my application. Use system time to create
id, which is the best fit, but I don't know how to do it. Please help.

Thanks in advance for any input.
 
D

Dave

GUID is a good way to get a unique id but if you want your id to have some
relationship to the time why not try using the number of days, minutes, or
seconds since the file was created until it was last updated? Actually, I
beleive the applications productBuildPart information gives you the number
of days since January 1, 2000 to the last day the file was modified?

System.Diagnostics.FileVersionInfo.GetVersionInfo( _
System.Reflection.Assembly.GetExecutingAssembly.Location).ProductBuildPart

If you want the time, in this case using minutes, between when the file was
created to when the file was last modified try -

System.IO.File.GetLastWriteTime( _
System.Reflection.Assembly.GetExecutingAssembly.Location).Subtract( _
System.IO.File.GetCreationTime( _
System.Reflection.Assembly.GetExecutingAssembly.Location)).TotalMinutes

Dave
 
H

Herfried K. Wagner [MVP]

Nina said:
I need to generate unique id for my application. Use system time to create
id, which is the best fit, but I don't know how to do it.

The simplest solution is using a GUID:

\\\
Dim ID As String = Guid.NewGuid().ToString()
///
 
G

Guest

Thank you all for ideas and suggestions. Please tell me how does GUID work?
In case, user exits the application, and lunches the application again later,
could GUID generate same id that was generated in previous session? If this
could happen then I cannot use it.
 
H

Herfried K. Wagner [MVP]

Nina said:
Thank you all for ideas and suggestions. Please tell me how does GUID
work?
In case, user exits the application, and lunches the application again
later,
could GUID generate same id that was generated in previous session? If
this
could happen then I cannot use it.

That's very, very, very, ..., very unlikely. In practice it's "guaranteed"
that every GUID only occurs once. There are various parameters used to
calculate the GUID. So you can use a GUID as ID without any fear that
you'll get a GUID twice.
 
G

Guest

Thanks again. The Id that generated by GUID is very long. Is there any way
that I can generate a unique id with a specific length?
 
G

Greg Burns

If you truncated it, wouldn't that introduce the possiblity (albeit slight)
of non-uniqueness?

What if you didn't pass it around as a string, but kept it in its native
datatype? It is only 16 bytes in SQL server when stored as uniqueidentifier
datatype. Versus the ~36 bytes for the equivalent string.

Greg
 
J

Jay B. Harlow [MVP - Outlook]

Nina,
Yes. Considering the entire GUID is what is guaranteed on being unique.

GUID's are not sequential, they are seemingly random numbers, based on a
complicated formula that ensures uniqueness, so I'm not sure where or how
Marina is thinking you can truncate it.

Although GUID's are numbers I do not consider them numbers in the same sense
as Integer, Double, or Decimal, as they are actually an identifier, a
Globally Unique IDentifier. Some articles use the term universally unique
identifier.

Hope this helps
Jay
 
M

Marina

Since the whole GUID is completely random and unique, a subset of it should
be pretty random as well. Obviously, as you have less characters in a
string, the odds of getting repeats increases. However, if you still have 16
characters, the odds of this happening are still pretty low.
 
J

Jay B. Harlow [MVP - Outlook]

Marina,
I still don't follow you. Do you plan on taking the first n characters, the
last n, the middle n, or a random sampling of n characters? Which sequence
(or collection) of n characters of a GUID do you think will even come close
to ensuring uniqueness? Without relying on an implementation detail of the
current implementation of GUID!

IMHO Any number other then all of them is simply a fragile design! (in other
words, as I stated, the GUID itself is what ensures uniqueness, not a subset
of it).

Also remember that a GUID is a structure that represents a 128-bit integer,
not a set of characters, the set of characters is just the human readable
representation of that structure.

NOTE: Reading
http://msdn.microsoft.com/library/d...n-us/cpref/html/frlrfsystemguidclasstopic.asp
even the full GUID has "a very low probability of being duplicated", which
means that Guid.NewGuid could return a duplicate...

Hope this helps
Jay
 
M

Marina

I would say that taking the first 16 characters, for example, will more or
less do it. It's not like GUIDs are based on something where it is just the
last characters that keep changing or something.

In theory, it may be that taking the first 16 characters would not guarantee
anything due to the GUID implementation. Yes, there might be some minute
possibility of a duplicate. But at some point you have to be practical.

And in practice, I think you will find that doing something like taking the
first 16 or 20 characters, will suffice.

And if the minute possibility that you might encounter a duplicate is still
troublesome because the system is so huge and critical, then the key column
for the table needs to be increased to support a GUID.
 
J

JohnBoy

Dim dt As DateTime = DateTime.Now
str = dt.ToString("MdyyHmsfff")

Guaranteed (provided the clock works) to be unique for 100 years or your
money back!
 
J

Jay B. Harlow [MVP - Outlook]

Marina,
Why mostly the first half, instead of mostly the second half or even mostly
the middle half? Isn't which half an implementation detail that might
change? Especially considering the change to CoCreateGUID from NT 4.0 to
Windows 2000, as suggested by:
http://support.microsoft.com/default.aspx?scid=kb;en-us;275280

Which GUID format are you suggesting using ("N", "D", "B", or "P")? As each
one causes different delimiters to be included.

For the different GUID formats & their lengths try:

Dim aGuid As Guid = Guid.NewGuid
Debug.WriteLine(aGuid.ToString("N"), "N")
Debug.WriteLine(aGuid.ToString("N").Length, "N")
Debug.WriteLine(aGuid.ToString("D"), "D")
Debug.WriteLine(aGuid.ToString("D").Length, "D")
Debug.WriteLine(aGuid.ToString("B"), "B")
Debug.WriteLine(aGuid.ToString("B").Length, "B")
Debug.WriteLine(aGuid.ToString("P"), "P")
Debug.WriteLine(aGuid.ToString("P").Length, "P")
Return

Remember a Guid is 128-bit (16 byte) value, so taking the first 16
characters of a GUID just doubled the memory you are using! (16 Chars = 32
Bytes)

Hence I find it safest to simply use the full 16-byte GUID, as again I would
not rely on IMHO a fragile design!

BTW: The following KB article is interesting:

http://support.microsoft.com/default.aspx?scid=kb;en-us;320375

I would considering using one of those the routines presented to create a
short GUID, instead of slicing & dicing System.Guid.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

John,
KA BOOM!!!! Your sample just gave me hundreds of duplicate entries!

At the very least you may want to include ALL 7 digits for milliseconds!
Plus full width (both digits) for months, days, hours, minutes & seconds!
str = dt.ToString("MMddyyHHmmssfffffff")

As your string will give duplicates on for Jan 11th & Nov 1st, plus it will
give duplicates between for 11:01 & 1:11!

To see what I mean try the following:

Dim dt1 As DateTime = #1/11/2005 11:01:00 AM#
Dim dt2 As DateTime = #11/1/2005 1:11:00 AM#
Dim str1 As String = dt1.ToString("MdyyHmsfff")
Dim str2 As String = dt2.ToString("MdyyHmsfff")

Debug.WriteLine(str1, dt1.ToString())
Debug.WriteLine(str2, dt2.ToString())

Debug.WriteLine(str1 = str2)


Of course when the clock falls back for daylight savings time, there will be
an hour where duplicates are also created! Also if you explicitly adjust
your clock or Windows adjusts your clock, you will have another opportunity
for duplicates.

My money is on using the full "precision" of System.Guid.

Hope this helps
Jay
 
J

Jay B. Harlow [MVP - Outlook]

I should add that if you need a string, and you want to ensure its as short
as possible I would look at encoding the (entire) Guid.ToByteArray value in
a format other then hex, possible base64 or some custom scheme that used
upper & lower case "ASCII" letters & numbers...

Similar to the GuidTest3.asp & GuidTest4.asp samples at
http://support.microsoft.com/default.aspx?scid=kb;en-us;320375

Hope this helps
Jay
 
M

Marina

I didn't say it has to be the first half. Take any part of it you like.
Geez. Are you just picking at everything and making it seem more
complicated then it is for a reason? Take some part of it that you like, get
rid of all delimiters, and in a practical case, you are going to get unique
strings.

Who cares about using slightly more memory or whatever? This wasn't supposed
to be the most memory efficient method of making sure you save every bit of
memory possible. It was a quick and easy way to generate a random string to
be used as a primary key. And like I said, in practical scenarios, this
works quite well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top