c# - 如何获取.NET 6.0 中的时钟序列?

标签 c# .net

我想创建自己的自定义 UUID 并希望在其中使用时钟序列,因为我不知道系统上有什么其他东西可以自动递增,即使系统时间被设置回来。

但是如何获取 .NET 6.0 中的时钟序列?

编辑:不寻找如何创建 GUID 或为什么我想这样做的问题的答案。问题是:如何获取.NET 6.0 中的时钟序列?

最佳答案

正如 user3112728 所指出的在 this回复:

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

有给定GUID的时钟序列,它是the forth block ,您可以或多或少优雅地检索它:

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

但是不存在时钟序列这样的东西(如 eidos )。
GUID 的第四个 block 是由一个算法生成的,该算法应遵循一些规则,但这些规则的限制不足以使实现具有唯一性,基于 this post这些是:

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.

所以基本上,如果您想实现一个时钟序列算法,您必须存储一个值并在发生这些条件之一时更改它。
只需更改它部分是松散的,这就是为什么没有独特的实现。

你可以选择这样的东西:

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

但是,我不认为重新发明有什么意义 the wheel在这里。
更新: Here , herehere 顺序 GUID 的一些信息/实现;

关于c# - 如何获取.NET 6.0 中的时钟序列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74559823/

相关文章:

c# - Foo().Result 和 Task.Run(() => Foo()).Result 在 C# 中有什么区别?

c# - ProgressBar 值中的标签未出现

c# - ReSharper 7.1 对象初始化程序格式

.net - Windows 服务在启动期间挂起

c# - 渲染多个日历使 html 膨胀,使页面陷入困境

c# - WPF - 在 UserControl 中绑定(bind) ObservableCollection 依赖属性

c# 访问所有可用的 css 列表或属性集合

c# - 我可以在 .NET 中使用 FileInfo.CopyTo() 显示文件复制进度吗?

c# - 检索 Windows 版本 "1511"

c# - 禁止覆盖派生类中的方法