且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C# DateTime.Now 精度

更新时间:2023-01-28 09:48:26

为什么 DateTime.Now 不如大多数 CPU 时钟可以处理的精确?

Why would DateTime.Now be made less precise than what most CPU clocks could handle?

一个好的时钟应该精确准确;那些是不同的.正如古老的笑话所说,停止的时钟一天准确两次,慢一分钟的时钟在任何时候都不准确.但是慢一分钟的时钟总是精确到最近的一分钟,而停止的时钟根本没有任何有用的精度.

A good clock should be both precise and accurate; those are different. As the old joke goes, a stopped clock is exactly accurate twice a day, a clock a minute slow is never accurate at any time. But the clock a minute slow is always precise to the nearest minute, whereas a stopped clock has no useful precision at all.

为什么 DateTime 应该精确到,比如说微秒,而它不可能精确到微秒?大多数人没有任何精确到微秒的官方时间信号的来源.因此,在精度的小数点后给出六位数字,其中最后五位是垃圾将是撒谎.

Why should the DateTime be precise to, say a microsecond when it cannot possibly be accurate to the microsecond? Most people do not have any source for official time signals that are accurate to the microsecond. Therefore giving six digits after the decimal place of precision, the last five of which are garbage would be lying.

请记住,DateTime 的目的是表示日期和时间.高精度计时根本不是 DateTime 的目的;正如您所注意到的,这就是 StopWatch 的目的.DateTime 的用途是表示日期和时间,例如向用户显示当前时间、计算到下周二的天数等.

Remember, the purpose of DateTime is to represent a date and time. High-precision timings is not at all the purpose of DateTime; as you note, that's the purpose of StopWatch. The purpose of DateTime is to represent a date and time for purposes like displaying the current time to the user, computing the number of days until next Tuesday, and so on.

简而言之,现在几点了?"以及那花了多长时间?"是完全不同的问题;不要使用旨在回答一个问题的工具来回答另一个问题.

In short, "what time is it?" and "how long did that take?" are completely different questions; don't use a tool designed to answer one question to answer the other.

感谢您的提问;这将是一篇很好的博客文章!:-)

Thanks for the question; this will make a good blog article! :-)