在 C# 中获取即时时间的最佳方法是什么?

What is the most optimal way of getting the Instant time in C#?

我正在开发一个代码库,他们使用两种不同的方法使用 C# 从 NodaTime 获取即时时间。

第一种方法是使用具有 IClock

的对象
IClock.GetCurrentInstant()

第二种方法是使用 Instant.FromDateTimeUtc and DateTime.UtcNow

Instant.FromDateTimeUtc(DateTime.UtcNow)

哪个是最佳做法?

引用NodaTime API reference

IClock is intended for use anywhere you need to have access to the current time.

Instant.FromDateTimeUtc 方法看起来旨在作为一种将现有 DateTime 结构转换为 Instant 格式的方法,而不是获取当前时间的正确方法。所以,根据 API 参考,你应该使用 IClock 方法来达到你上面提到的目的。