2

I would like to create my own custom UUID and would like to use the clock sequence in it because I know nothing else on my system that auto increments even when the system time is set back.

But how do I get the clock sequence in .NET 6.0?

Edit: Not looking for answers to the question how to create a GUID or why I would want to do that. The question is: How do I get the clock sequence in .NET 6.0?

Marc van Nieuwenhuijzen
  • 1,637
  • 1
  • 14
  • 22

1 Answers1

3

As pointed out by user3112728 in this response:

"Clock Sequence" seems like a really misleading name. Based on its definition, a better name might be "Random Component of uuid".

There is the clock sequence of a given GUID, it's the forth block, you can retrieve it more or less gracefully:

Console.WriteLine(Guid.NewGuid().ToString().Split('-')[3]);

But there is no such a thing as the clock sequence (as an eidos).
The 4th block of a GUID is generated by an algorithm that should follow some rules, but these are not restrictive enough for the implementation to be unique, based on this post those are:

The RFC-4122 says that the clock sequence must be changed when:

the clock is backwards;
the clock might have been set backwards;
the generator is not sure if the clock is backwards or not;
the node identifier changed;
node identifiers are getting mixed up.

So basically, if you want to implement a clock sequence algorithm, you have to store a value and change it whenever one of these conditions occur.
The just change it part is loose, that why there is no unique implementation.

You can go with something like this:

// Change clock sequence because clock is backwards
// This is a definitely incomplete example
if (now <= lastTime) {
  clockSequence++
}

However, I don't see the point of reinventing the wheel here.
Update: Here, here and here some info/implementations of sequential GUIDs;

Orace
  • 7,822
  • 30
  • 45
  • Thx. But this won't work between restarts. I'm trying to create my own sequential guid. Guid.NewGuid is not sequential. – Marc van Nieuwenhuijzen Nov 28 '22 at 14:31
  • I'm not looking for a different solution. I can think of one myself. I'm looking for an answer to this question though. I would like to know if it is possible in .NET core to get the current clock sequence. Why I need is irrelevant to the question since other people might be looking for the same answer for different reasons. – Marc van Nieuwenhuijzen Nov 28 '22 at 16:09
  • ok. I just figured that the current clock sequence could have been some CPU firmware functionality. Gues i figured wrong then. Thanks for your answer – Marc van Nieuwenhuijzen Nov 28 '22 at 16:46
  • 1
    Maybe [this](https://stackoverflow.com/questions/41899426) can help you. I updated my answer to include my previous comments. Again, why you need this may be help-full for future answers. – Orace Nov 28 '22 at 16:52