Is there a high resolution (microsecond, nanosecond) DateTime object available for the CLR?

前端 未结 6 1352
情深已故
情深已故 2020-12-08 05:02

I have an instrument that stores timestamps the microsecond level, and I need to store those timestamps as part of collecting information from the instrument. Note that I do

6条回答
  •  盖世英雄少女心
    2020-12-08 05:33

    Looking at the answers, and the DateTime.Ticks property, it's possible to calculate Microseconds and Nanoseconds from the given values. As a result, I put together this extension method class to do it. (Sadly, I don't think I'll be able to use it given some other requirements, but other people may find it useful.)

    /// 
    /// Extension methods for accessing Microseconds and Nanoseconds of a
    /// DateTime object.
    /// 
    public static class DateTimeExtensionMethods
    {
       /// 
       /// The number of ticks per microsecond.
       /// 
       public const int TicksPerMicrosecond = 10;
       /// 
       /// The number of ticks per Nanosecond.
       /// 
       public const int NanosecondsPerTick = 100;
    
       /// 
       /// Gets the microsecond fraction of a DateTime.
       /// 
       /// 
       /// 
       public static int Microseconds(this DateTime self)
       {
          return (int)Math.Floor(
             (self.Ticks 
             % TimeSpan.TicksPerMillisecond )
             / (double)TicksPerMicrosecond);
       }
       /// 
       /// Gets the Nanosecond fraction of a DateTime.  Note that the DateTime
       /// object can only store nanoseconds at resolution of 100 nanoseconds.
       /// 
       /// The DateTime object.
       /// the number of Nanoseconds.
       public static int Nanoseconds(this DateTime self)
       {
          return (int)(self.Ticks % TimeSpan.TicksPerMillisecond % TicksPerMicrosecond)
             * NanosecondsPerTick;
       }
       /// 
       /// Adds a number of microseconds to this DateTime object.
       /// 
       /// The DateTime object.
       /// The number of milliseconds to add.
       public static DateTime AddMicroseconds(this DateTime self, int microseconds)
       {
          return self.AddTicks(microseconds * TicksPerMicrosecond);
       }
       /// 
       /// 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().
       /// 
       /// The DateTime object.
       /// The number of nanoseconds to add.
       public static DateTime AddNanoseconds(this DateTime self, int nanoseconds)
       {
          return self.AddTicks((int)Math.Round(nanoseconds / (double)NanosecondsPerTick));
       }
    }
    

    This still won't let you set the Microseconds or Nanoseconds upon creation, but they can be added shortly after. It also doesn't give resolution better than what a DateTime can (eg, 1/10 of a microsecond aka 100 nanosecond resolution.)

    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
    

    Here's hoping this works for someone else!

提交回复
热议问题