Currently, the value of a single Tick in .NET is 100ns, however there is no API surface for accessing microseconds or nanoseconds other than using Ticks to manually calculate these numbers. In order to allow for working with these higher precision times, I am proposing the following new APIs:
Approved API
namespaceSystem{publicstructDateTime{/// <exception cref="ArgumentOutOfRangeException">When microsecond is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond);/// <exception cref="ArgumentOutOfRangeException">When microsecond is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond,System.DateTimeKindkind);/// <exception cref="ArgumentOutOfRangeException">When microsecond is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond,System.Globalization.Calendarcalendar);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}/// <exception cref="ArgumentOutOfRangeException">The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.</exception>publicDateTimeAddMicroseconds(doublevalue);}publicstructDateTimeOffset{/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTimeOffset(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond,System.TimeSpanoffset);/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTimeOffset(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond,System.TimeSpanoffset,System.Globalization.Calendarcalendar);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}/// <exception cref="ArgumentOutOfRangeException">The resulting DateTimeOffset is less than DateTimeOffset.MinValue or greater than DateTimeOffset.MaxValue.</exception>publicDateTimeOffsetAddMicroseconds(doublevalue);}publicstructTimeSpan{publicconstlongTicksPerMicrosecond=10L;publicconstlongNanosecondsPerTick=100L;/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicTimeSpan(intdays,inthours,intminutes,intseconds,intmilliseconds,intmicroseconds);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicroseconds{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanoseconds{get;}/// <returns>The total number of microseconds represented by this instance.</returns>publicdoubleTotalMicroseconds{get;}/// <returns>The total number of nanoseconds represented by this instance.</returns>publicdoubleTotalNanoseconds{get;}/// <exception cref="OverflowException">When value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.</exception>publicstaticTimeSpanFromMicroseconds(doublemicroseconds);}publicstructTimeOnly{/// <param name="microsecond">The microsecond (0 through 999).</param>/// <exception cref="ArgumentOutOfRangeException">When microsecond is not between 0 and 999.</exception>publicTimeOnly(intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond);/// <returns>The microsecond component of the time represented by this instant, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanosecond component of the time represented by this instant, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}}}Original API Proposal
namespaceSystem{publicstructDateTime{/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicroseconds);/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicroseconds,System.DateTimeKindkind);/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTime(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicroseconds,System.Globalization.Calendarcalendar);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}/// <exception cref="ArgumentOutOfRangeException">The resulting DateTime is less than DateTime.MinValue or greater than DateTime.MaxValue.</exception>publicDateTimeAddMicroseconds(intmicroseconds);}publicstructDateTimeOffset{/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTimeOffset(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicroseconds,System.TimeSpanoffset);/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicDateTimeOffset(intyear,intmonth,intday,inthour,intminute,intsecond,intmillisecond,intmicroseconds,System.TimeSpanoffset,System.Globalization.Calendarcalendar);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}/// <exception cref="ArgumentOutOfRangeException">The resulting DateTimeOffset is less than DateTimeOffset.MinValue or greater than DateTimeOffset.MaxValue.</exception>publicDateTimeOffsetAddMicroseconds(intmicroseconds);}publicstructTimeSpan{publicconstlongTicksPerMicrosecond=10L;publicconstdoubleTicksPerNanosecond=0.01D;/// <exception cref="ArgumentOutOfRangeException">When microseconds is not between 0 and 999.</exception>publicTimeSpan(intdays,inthours,intminutes,intseconds,intmilliseconds,intmicroseconds);/// <returns>The microseconds component, expressed as a value between 0 and 999.</returns>publicintMicroseconds{get;}/// <returns>The nanoseconds component, expressed as a value between 0 and 900.</returns>publicintNanoseconds{get;}/// <returns>The total number of microseconds represented by this instance.</returns>publicdoubleTotalMicroseconds{get;}/// <returns>The total number of nanoseconds represented by this instance.</returns>/// <exception cref="OverflowException">When value starts to approach TimeSpan.MinValue or TimeSpan.MaxValue.</exception>publicdoubleTotalNanoseconds{get;}/// <exception cref="OverflowException">When value is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue.</exception>publicstaticTimeSpanFromMicroseconds(doublevalue);}publicstructTimeOnly{/// <param name="microsecond">The microsecond (0 through 999).</param>/// <exception cref="ArgumentOutOfRangeException">When microsecond is not between 0 and 999.</exception>publicTimeOnly(intday,inthour,intminute,intsecond,intmillisecond,intmicrosecond);/// <returns>The microsecond component of the time represented by this instant, expressed as a value between 0 and 999.</returns>publicintMicrosecond{get;}/// <returns>The nanosecond component of the time represented by this instant, expressed as a value between 0 and 900.</returns>publicintNanosecond{get;}}}Example Usage
publicclassSamples{publicvoidDateTimeSamples(){newDateTime(0001,01,01,00,00,00,00,999).Ticks;// 9990DateTime.Parse("0001-01-01 00:00:00.0009990").Microsecond;// 999DateTime.Parse("0001-01-01 00:00:00.0000009").Nanosecond;// 900DateTime.Zero.AddMicroseconds(999).Ticks;// 9990}publicvoidDateTimeOffsetSamples(){newDateTimeOffset(0001,01,01,00,00,00,00,999,TimeSpan.FromHours(-7)).Ticks;// 9990DateTimeOffset.Parse("0001-01-01 00:00:00.0009990 -7").Microsecond;// 999DateTimeOffset.Parse("0001-01-01 00:00:00.0000009 -7").Nanosecond;// 900newDateTimeOffset().AddMicroseconds(999).Ticks;// 9990}publicvoidTimeSpanSamples(){newTimeSpan(999*TimeSpan.TicksPerMicrosecond).Ticks;// 9990newTimeSpan((long)(900*TimeSpan.TicksPerNanosecond)).Ticks;// 9newTimeSpan(0001,01,01,00,00,00,00,999).Ticks;// 9990TimeSpan.Parse("0001-01-01 00:00:00.0009990").Microsecond;// 999TimeSpan.Parse("0001-01-01 00:00:00.0000009").Nanosecond;// 900TimeSpan.Zero.AddMicroseconds(999).Ticks;// 9990}}Notes
TimeSpan.TotalMicroseconds and TimeSpan.TotalNanoseconds return double still since that is what the other Total properties return. This could be long but I kept it as double for consistency.TimeSpan.FromMicroseconds takes a double to keep the API consistent with the other From methods.
Currently, the value of a single
Tickin .NET is 100ns, however there is no API surface for accessing microseconds or nanoseconds other than usingTicksto manually calculate these numbers. In order to allow for working with these higher precision times, I am proposing the following new APIs:Approved API
Original API Proposal
Example Usage
Notes
TimeSpan.TotalMicrosecondsandTimeSpan.TotalNanosecondsreturn double still since that is what the otherTotalproperties return. This could be long but I kept it as double for consistency.TimeSpan.FromMicrosecondstakes a double to keep the API consistent with the otherFrommethods.