是否有可用于CLR的高分辨率(微秒,纳秒)DateTime对象?

Rob*_*t P 26 .net c# clr datetime timestamp

我有一个仪器存储时间戳微秒级别,我需要存储这些时间戳作为从仪器收集信息的一部分.请注意,我并不需要生成时间戳 ; 这些时间戳是由仪器本身使用高分辨率实时操作系统预先生成的.解析这些值不是问题 - 它们使用UTC时间的标准格式存储.本来我想用C#DateTime结构只能存储时间戳达毫秒级的分辨率.

是否有另一个.NET提供的对象或一个支持微观和(理想情况下)纳秒分辨率时间戳的通用C#库,或者我将不得不自己动手?

ste*_*nar 13

DateTime毕竟你可以使用.DateTime.Ticks'分辨率是100纳秒.您可以设置刻度线DateTime.AddTicks.

  • 是的,但他明确表示他需要微秒,而*理想情况下需要*纳秒.这就是为什么我用"可能"来表达它. (3认同)
  • @PeterMortensen - 你有什么尝试?我的猜测是你尝试过Sleep(1),它只能给你一个大约16毫的分辨率.但是,Ticks的分辨率为每秒10,000,000. (3认同)

Rob*_*t P 11

查看答案和DateTime.Ticks属性,可以从给定值计算微秒和纳秒.结果,我把这个扩展方法类放在一起来做.(可悲的是,我不认为我可以在其他要求的情况下使用它,但其他人可能会发现它很有用.)

/// <summary>
/// Extension methods for accessing Microseconds and Nanoseconds of a
/// DateTime object.
/// </summary>
public static class DateTimeExtensionMethods
{
   /// <summary>
   /// The number of ticks per microsecond.
   /// </summary>
   public const int TicksPerMicrosecond = 10;
   /// <summary>
   /// The number of ticks per Nanosecond.
   /// </summary>
   public const int NanosecondsPerTick = 100;

   /// <summary>
   /// Gets the microsecond fraction of a DateTime.
   /// </summary>
   /// <param name="self"></param>
   /// <returns></returns>
   public static int Microseconds(this DateTime self)
   {
      return (int)Math.Floor(
         (self.Ticks 
         % TimeSpan.TicksPerMillisecond )
         / (double)TicksPerMicrosecond);
   }
   /// <summary>
   /// Gets the Nanosecond fraction of a DateTime.  Note that the DateTime
   /// object can only store nanoseconds at resolution of 100 nanoseconds.
   /// </summary>
   /// <param name="self">The DateTime object.</param>
   /// <returns>the number of Nanoseconds.</returns>
   public static int Nanoseconds(this DateTime self)
   {
      return (int)(self.Ticks % TimeSpan.TicksPerMillisecond % TicksPerMicrosecond)
         * NanosecondsPerTick;
   }
   /// <summary>
   /// Adds a number of microseconds to this DateTime object.
   /// </summary>
   /// <param name="self">The DateTime object.</param>
   /// <param name="microseconds">The number of milliseconds to add.</param>
   public static DateTime AddMicroseconds(this DateTime self, int microseconds)
   {
      return self.AddTicks(microseconds * TicksPerMicrosecond);
   }
   /// <summary>
   /// Adds a number of nanoseconds to this DateTime object.  Note: this
   /// object only stores nanoseconds of resolutions of 100 seconds.
   /// Any nanoseconds passed in lower than that will be rounded using
   /// the default rounding algorithm in Math.Round().
   /// </summary>
   /// <param name="self">The DateTime object.</param>
   /// <param name="nanoseconds">The number of nanoseconds to add.</param>
   public static DateTime AddNanoseconds(this DateTime self, int nanoseconds)
   {
      return self.AddTicks((int)Math.Round(nanoseconds / (double)NanosecondsPerTick));
   }
}
Run Code Online (Sandbox Code Playgroud)

这仍然不允许您在创建时设置微秒或纳秒,但可以在不久之后添加它们.它也不能提供比DateTime更好的分辨率(例如,1/10微秒,即100纳秒分辨率.)

DateTime time = new DateTime(year, month, day, hour, min, sec, msec);
time = time.AddMicroseconds(microseconds);
time = time.AddNanoseconds(nanoseconds); # note: rounds if not enough added
Run Code Online (Sandbox Code Playgroud)

这里希望这适用于其他人!


Son*_*nül 5

使用.NET 7,您可以顺便使用DateTime.Microsecond和/或属性,而无需创建自己的扩展方法,但它们仍然提供与 .NET 相同的精度。DateTime.NanosecondTicks

\n
\n

在 .NET 7 之前,各种日期和时间结构中可用的最小时间增量是属性中可用的“刻度” Ticks。作为参考,单个滴答时间为 100ns。开发人员传统上必须对“tick”值进行计算才能确定微秒和纳秒值。在 .NET 7 中,我们\xe2\x80\x99\n在日期和时间实现中引入了微秒和纳秒。

\n
\n