vb.net 为什么 DateTime 基于刻度而不是毫秒?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14415555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:56:50  来源:igfitidea点击:

Why is DateTime based on Ticks rather than Milliseconds?

c#.netvb.netdatetime

提问by mas

Why is the minimum resolution of a DateTimebased on Ticks (100-nanosecond units) rather than on Milliseconds?

为什么 a 的最小分辨率DateTime基于 Ticks(100 纳秒单位)而不是毫秒?

回答by CodesInChaos

  • TimeSpanand DateTimeuse the same Ticksmaking operations like adding a TimeSpanto a DateTimetrivial.
  • More precision is good. Mainly useful for TimeSpan, but above reason transfers that to DateTime.

    For example StopWatchmeasures short time intervals often shorter than a millisecond. It can return a TimeSpan.
    In one of my projects I used TimeSpanto address audio samples. 100ns is short enough for that, milliseconds wouldn't be.

  • Even using milliseconds ticks you need an Int64 to represent DateTime. But then you're wasting most of the range, since years outside 0 to 9999 aren't really useful. So they chose ticks as small as possible while allowing DateTimeto represent the year 9999.

    There are about 261.5ticks with 100ns. Since DateTimeneeds two bits for timezone related tagging, 100ns ticks are the smallest power-of-ten interval that fits an Int64.

  • TimeSpanDateTime使用相同的Ticks制作操作,例如将 a 添加TimeSpanDateTime平凡中。
  • 精度越高越好。主要用于TimeSpan,但以上原因将其转移到DateTime

    例如,StopWatch测量通常短于一毫秒的短时间间隔。它可以返回一个TimeSpan.
    在我的一个项目中,我曾经TimeSpan处理音频样本。100ns 足够短,毫秒就不行了。

  • 即使使用毫秒刻度,您也需要一个 Int64 来表示DateTime. 但是,您浪费了大部分范围,因为 0 到 9999 之外的年份并不是真正有用的。所以他们选择了尽可能小的刻度,同时允许DateTime代表 9999 年。

    100ns大约有 2 61.5 个滴答声。由于DateTime时区相关标记需要两位,因此 100ns 滴答是适合 Int64 的最小十次幂间隔。

So using longer ticks would decrease precision, without gaining anything. Using shorter ticks wouldn't fit 64 bits. => 100ns is the optimal value given the constraints.

因此,使用更长的刻度会降低精度,而不会获得任何收益。使用较短的刻度不适合 64 位。=> 100ns 是给定约束的最佳值。

回答by Erix

for higher time resolution, even though you don't need it most of the time.

以获得更高的时间分辨率,即使您大部分时间都不需要它。

回答by Soner G?nül

From MSDN;

来自MSDN;

A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.

一个滴答声代表一百纳秒或百万分之一秒。一毫秒有 10,000 个滴答声。

A tick represents the total number of ticks in local time, which is midnight on January 1st in the year 0001. But a tick is also smallest unit for TimeSpanalso. Since ticks are Int64, so if miliseconds used instead of ticks, there can be a information losing.

一个刻度代表本地时间的总刻度数,即0001年1月1日午夜。但刻度也是also的最小单位TimeSpan。由于刻度是Int64,因此如果使用毫秒而不是刻度,则可能会丢失信息。

Also could be a default CLSimplementation.

也可以是默认CLS实现。

回答by GeorgeVremescu

The tick is what the system clock works with.

滴答声是系统时钟所使用的。