Instant.Now为NodaTime [英] Instant.Now for NodaTime

查看:561
本文介绍了Instant.Now为NodaTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让使用野田佳彦时间框架,乔恩斯基特手柄(和其他人)。

I'm trying to get a handle on using the Noda Time framework by Jon Skeet (and others).

我想存储当前现在(立即)。 ?即时从长期蜱创建的,但什么是目前现在算蜱

I'm trying to store the current now(Instant). Instant is created from a long ticks, but what is the current now count of Ticks?

它是:

Instant now = new Instant(DateTime.Now.ToUniversalTime().Ticks);

和或?

Instant now = Instant.FromDateTimeUtc(DateTime.Now.ToUniversalTime());



他们是等价的,我是连这样做对吗?

Are they equivalent, am I even doing this right?

PS,如果乔恩回答的这个 - 我想提出一个Instant.Now财产

PS, if Jon answer's this - I'd like to propose an Instant.Now property.

PS2我知道标题中包含一个标签,但它不会让我有一个简短的Instant.Now称号。

PS2 I know the title contains a tag, but it wouldn't let me have a short "Instant.Now" title.

推荐答案

我做了一些研究,似乎的NodaTime方式是获得的现在根据时钟

I did a bit of research and it seems that the NodaTime way is to get the now moment according to a clock.

如果你想使用系统时钟来获取当前时间,只需使用 SystemClock.Instance.Now

If you want to get the current time using the system clock, just use SystemClock.Instance.Now.

,而不是使用 SystemClock.Instance 直接在你的代码,这是最好的你注入在时间感知类的 IClock 依赖。

However, instead of using the SystemClock.Instance directly in your code, it's preferable that you inject an IClock dependency in your time-aware classes.

这将允许您:


  • 提供 SystemClock.Instance 类在运行时,所以代码将使用正确的时间

  • 单元测试过程中提供假的实施 IClock 的,让你来调整时间需要的,以便测试不同的情形(如时间的流逝)。有一个NodaTime.Testing项目,提供了这样一个类,名为 FakeClock

  • provide the class with SystemClock.Instance at runtime, so the code will use the correct time
  • supply a fake implementation of IClock during unit testing to allow you to tweak the time as needed in order to test various scenarios (like the passing of time). There's a NodaTime.Testing project that offers such a class, called FakeClock.

我觉得这是非常有用的。我认为有像新的即时() Instant.Now 返回当前时间会更容易硬编码惯例的 SystemClock 下的封面,因此错过了测试优势NodaTime优惠。

I find this very useful. I think having something like new Instant() or Instant.Now return the current time would make it easier to hardcode usages of SystemClock under the covers, therefore missing the testing advantage that NodaTime offers.

有关单元测试的详细信息与NodaTime,请参见此链接

For more info on unit testing with NodaTime, see this link.

关于你的代码示例:他们是不等价的。

Regarding your code examples: they are not equivalent.


  • <$ C。 $ C> Instant.FromDateTimeUtc(DateTime.Now.ToUniversalTime())确实会给你UTC当前的时刻。

  • 新即时(DateTime.Now.ToUniversalTime()。蜱)会给你一个错误的日期遥远的未来,因为首创置业的的 DateTime.Ticks 表示从 1/1/0001 和NodaTime的 Instant.Ticks 代表蜱的数量,因为 1/1/1970 (见备注<一个HREF =http://noda-time.googlecode.com/hg/docs/api/html/M_NodaTime_Instant__ctor.htm相对=nofollow>这里)。

  • Instant.FromDateTimeUtc(DateTime.Now.ToUniversalTime()) will indeed give you the current instant in UTC.
  • new Instant(DateTime.Now.ToUniversalTime().Ticks) will give you a wrong date far in the future, because the BCL's DateTime.Ticks represents the number of ticks since 1/1/0001, and NodaTime's Instant.Ticks represents the number of ticks since 1/1/1970 (see the remark here).

这篇关于Instant.Now为NodaTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆