简体   繁体   English

DateTime.Now作为参数值C#

[英]DateTime.Now as parameter value C#

I have a method that has a parameter of type DateTime that must not be in the past, which indicates when some other stuff executes. 我有一个方法,其具有DateTime类型的参数,该参数不能在过去,这表示何时执行其他一些东西。 So naturally I want to use this method and pass DateTime.Now to it, so that everything happens as soon as possible. 所以很自然地我想使用这个方法并将DateTime.Now传递给它,以便一切尽快发生。

The problem here is that DateTime.Now at the time of the call and DateTime.Now at the time it is actually checked will probably be different values, so how do I make sure that the date is not in the past, yet allow the method to be called with something like DateTime.Now? 这里的问题是调用时的DateTime.Now和实际检查时的DateTime.Now可能是不同的值,所以我如何确保日期不是过去,但允许方法用DateTime.Now之类的东西来调用? I don't want to add magic numbers here, but a general solution. 我不想在这里添加幻数,而是一般的解决方案。

UPDATE. UPDATE。 I've found this question How frequent is DateTime.Now updated 我发现了这个问题DateTime.Now的频繁更新

It seems to me that you have two options: 在我看来,你有两个选择:

  1. Add an appropriate small amount of time on to now (1 second say) so that you'll trigger things almost immediately. 添加适当的少量时间到现在(1秒说),这样你几乎可以立即触发事情。 This will work, but its not nice. 这会奏效,但不好看。
  2. Add another method (overload) that doesn't take a time parameter but instead actions immediately eg 添加另一个不占用时间参数的方法(重载),而是立即执行操作,例如

    DoAction(attime); DoAction(attime); - will do the action at the stated time - 将在规定时间执行操作

    DoAction(); DoAction(); - will do the action now - 现在就行动吧

The latter is a nicer solution but obviously there may be issues with how/where the method is available for use (and assumes you can add the method anyway). 后者是一个更好的解决方案,但显然可能存在方法可供使用的方式/位置的问题(并假设您无论如何都可以添加方法)。

If you want everything to happen as soon as possible why don't you rewrite your method to run as soon as it's called, or create an overload without the DateTime parameter to run as soon as it's called. 如果你希望尽快发生所有事情,为什么不在调用它时重写你的方法来运行,或者在调用时立即创建一个没有DateTime参数运行的重载。

Using the overload of course would allow you to run your code at a time in the future (using the DateTime overload) or immediately. 当然,使用重载将允许您在将来的某个时间运行代码(使用DateTime重载)或立即运行。

这是一个哲学问题 - DateTime.Now总是在过去。

Compare the parameter on entry to DateTime.Now. 将条目上的参数与DateTime.Now进行比较。 If it's in the past, then use DateTime.Now. 如果它在过去,则使用DateTime.Now。

Alternately, don't use a parameter; 或者,不要使用参数; just use DateTime.Now. 只需使用DateTime.Now。

如果时间足够接近,测试时必须有一定的容差。

Let your function take a Nullable<DateTime> . 让你的函数采用Nullable<DateTime> Interpret a null time to mean "do this immediately". 将空时间解释为“立即执行此操作”。

If you check that the passed DateTime parameter is equal to or greater than DateTime.Now at the beginning of the function, it is extremely unlikely that the system time would have changed between the time the function was called with DateTime.now and when the check is made. 如果检查传递的DateTime参数是否等于或大于函数开头的DateTime.Now,则在使用DateTime.now调用函数和检查函数之间系统时间发生变化的可能性极小。是。 The system time is only updated every 10 or 15 milliseconds, and the actual function call time is much less than that. 系统时间仅每10或15毫秒更新一次,实际的函数调用时间远小于此值。

To be sure that this never happens, however, you would have to check the passed time against DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 0, 25)) , which would give the slight possibility of a time a little bit in the past being accepted. 但是,为了确保这种情况永远不会发生,你必须检查DateTime.Now.Subtract(new TimeSpan(0, 0, 0, 0, 25))的传递时间,这样可能会有一点时间a过去的一点点被接受了。

Throw an ArgumentException if your date arg is in the past? 如果你的日期arg是过去的话,抛出一个ArgumentException? This seems the simplest. 这似乎是最简单的。

For the parameter, use a delegate returning a DateTime value. 对于参数,请使用返回DateTime值的委托。 When calling the function, pass in a delegate pointing to a function that returns DateTime.Now . 调用该函数时,传入一个指向返回DateTime.Now的函数的委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM