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 not need to generate timestamps; these time stamps are pre-generated by the instrument itself using a high resolution real-time operating system. Parsing out these values is not an issue — they are stored using a standard format in UTC time. Originally I wanted to use the C# DateTime structure can only store time stamps up millisecond resolution.
我有一个工具可以存储微秒级的时间戳,我需要存储这些时间戳,作为收集工具信息的一部分。注意,我不需要生成时间戳;这些时间戳是由仪器本身使用高分辨率实时操作系统预先生成的。解析这些值不是问题——它们是在UTC时间使用标准格式存储的。最初我想使用c# DateTime结构,它只能存储毫秒级的时间戳。
Is there another object supplied with .NET or a common C# library that supports micro- and (ideally) nanosecond resolution timestamps, or am I going to have to roll my own?
是否有另一个由。net提供的对象或一个普通的c#库支持微秒和(理想情况下)纳秒分辨率的时间戳,或者我必须自己滚动自己的时间戳?
6 个解决方案
#1
11
You might be able to use DateTime
after all. DateTime.Ticks
' resolution is 100 nanoseconds. You can set the ticks with DateTime.AddTicks
.
毕竟,您可能可以使用DateTime。DateTime。蜱的分辨率是100纳秒。您可以使用DateTime.AddTicks设置刻度。
#2
10
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.)
看看答案和日期时间。滴答属性,可以从给定的值中计算微秒和纳秒。因此,我将这个扩展方法类组合在一起。(遗憾的是,鉴于其他一些需求,我认为我无法使用它,但其他人可能会发现它有用。)
/// <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));
}
}
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.)
这仍然不会让您在创建时设置微秒或纳秒,但是可以在创建后不久添加它们。它也不能提供比日期时间更好的分辨率(例如,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
Here's hoping this works for someone else!
希望这对别人有用!
#3
4
If I really needed more accuracy than the 100 ns resolution provided by DateTime
, I would consider creating a structure that contains a DateTime
and an integer value:
如果我真的需要比DateTime提供的100 ns分辨率更高的精度,我将考虑创建一个包含DateTime和一个整数值的结构:
public struct HiResDateTime
{
public HiResDateTime(DateTime dateTime, int nanoseconds)
{
if (nanoSeconds < 0 || nanoSeconds > 99)
throw new ArgumentOutOfRangeException(...);
DateTime = dateTime;
Nanoseconds = nanoseconds;
}
... possibly other constructors including one that takes a timestamp parameter
... in the format provided by the instruments.
public DateTime DateTime { get; private set; }
public int Nanoseconds { get; private set; }
... implementation ...
}
Then implement whatever is needed, for example:
然后实现任何需要的东西,例如:
- Comparison (
DateTime
first, thenNanoseconds
) - 比较(首先是时间,然后是纳秒)
-
ToString()
e.g. format DateTime to 100 ns accuracy then append nanoseconds. - ToString()例如,将DateTime格式设置为100ns精确度,然后附加纳秒。
- Conversion to/from
DateTime
- 转换到/从DateTime
- Add/subtract (might need a similar
HiResTimeSpan
) ... etc. ... - 加减(可能需要一个类似的HiResTimeSpan)……等……
#4
2
If you want something that operates on significant fractions of a microsecond, then No. The thing you're asking for doesn't exist as part of the standard libraries, but for what you're asking, why do you need this? It sounds like you really need two components, a string (variable length, hold's almost any conceivable value) and a DateTime for the UTC standard formatted date/time that you get natively.
如果你想要的是对一微秒的重要部分起作用的东西,那就不要。你想要的东西不是作为标准库的一部分存在的,但是你想问的是,为什么你需要这个?听起来您确实需要两个组件,一个字符串(可变长度,hold几乎是任何可能的值)和一个DateTime,用于您本地获得的UTC标准格式化日期/时间。
Micro/nano scale second timekeeping is not in the "normal" range of computations, so it's not provided in the "normal" .NET libraries.
微/纳米级的二次计时不属于“正常”计算范围,所以在“正常”. net库中没有提供。
What will you be doing with these timestamps? Will you be comparing them? Adding/subtracting them? I would suggest running reflector for the basic DateTime object (actually I think I'm gonna do that real quick too)
你打算用这些时间戳做什么?你会比较他们吗?增加/减少他们吗?我建议为基本的DateTime对象运行reflector(实际上我想我也会很快地这么做)
For your benefit, here's the simple version of the .NET Reflector disassembly of the standard DateTime object (and since the other answer at the time of this edit suggests the TimeSpan element, that as well)
为了您的利益,这里是标准DateTime对象的。net Reflector反汇编的简单版本(由于编辑时的另一个答案是TimeSpan元素)
[Serializable]
public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
{
// Fields
private ulong dateData;
private const string DateDataField = "dateData";
private const int DatePartDay = 3;
private const int DatePartDayOfYear = 1;
private const int DatePartMonth = 2;
private const int DatePartYear = 0;
private const int DaysPer100Years = 0x8eac;
private const int DaysPer400Years = 0x23ab1;
private const int DaysPer4Years = 0x5b5;
private const int DaysPerYear = 0x16d;
private const int DaysTo10000 = 0x37b9db;
private const int DaysTo1601 = 0x8eac4;
private const int DaysTo1899 = 0xa9559;
private static readonly int[] DaysToMonth365;
private static readonly int[] DaysToMonth366;
private const long DoubleDateOffset = 0x85103c0cb83c000L;
private const long FileTimeOffset = 0x701ce1722770000L;
private const ulong FlagsMask = 13835058055282163712L;
private const ulong KindLocal = 9223372036854775808L;
private const ulong KindLocalAmbiguousDst = 13835058055282163712L;
private const int KindShift = 0x3e;
private const ulong KindUnspecified = 0L;
private const ulong KindUtc = 0x4000000000000000L;
private const ulong LocalMask = 9223372036854775808L;
private const long MaxMillis = 0x11efae44cb400L;
internal const long MaxTicks = 0x2bca2875f4373fffL;
public static readonly DateTime MaxValue;
private const int MillisPerDay = 0x5265c00;
private const int MillisPerHour = 0x36ee80;
private const int MillisPerMinute = 0xea60;
private const int MillisPerSecond = 0x3e8;
internal const long MinTicks = 0L;
public static readonly DateTime MinValue;
private const double OADateMaxAsDouble = 2958466.0;
private const double OADateMinAsDouble = -657435.0;
private const long OADateMinAsTicks = 0x6efdddaec64000L;
private const long TicksCeiling = 0x4000000000000000L;
private const string TicksField = "ticks";
private const ulong TicksMask = 0x3fffffffffffffffL;
private const long TicksPerDay = 0xc92a69c000L;
private const long TicksPerHour = 0x861c46800L;
private const long TicksPerMillisecond = 0x2710L;
private const long TicksPerMinute = 0x23c34600L;
private const long TicksPerSecond = 0x989680L;
// Methods
static DateTime();
public DateTime(long ticks);
private DateTime(ulong dateData);
public DateTime(long ticks, DateTimeKind kind);
private DateTime(SerializationInfo info, StreamingContext context);
public DateTime(int year, int month, int day);
internal DateTime(long ticks, DateTimeKind kind, bool isAmbiguousDst);
public DateTime(int year, int month, int day, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);
public DateTime Add(TimeSpan value);
private DateTime Add(double value, int scale);
public DateTime AddDays(double value);
public DateTime AddHours(double value);
public DateTime AddMilliseconds(double value);
public DateTime AddMinutes(double value);
public DateTime AddMonths(int months);
public DateTime AddSeconds(double value);
public DateTime AddTicks(long value);
public DateTime AddYears(int value);
public static int Compare(DateTime t1, DateTime t2);
public int CompareTo(DateTime value);
public int CompareTo(object value);
private static long DateToTicks(int year, int month, int day);
public static int DaysInMonth(int year, int month);
internal static long DoubleDateToTicks(double value);
public bool Equals(DateTime value);
public override bool Equals(object value);
public static bool Equals(DateTime t1, DateTime t2);
public static DateTime FromBinary(long dateData);
internal static DateTime FromBinaryRaw(long dateData);
public static DateTime FromFileTime(long fileTime);
public static DateTime FromFileTimeUtc(long fileTime);
public static DateTime FromOADate(double d);
private int GetDatePart(int part);
public string[] GetDateTimeFormats();
public string[] GetDateTimeFormats(char format);
public string[] GetDateTimeFormats(IFormatProvider provider);
public string[] GetDateTimeFormats(char format, IFormatProvider provider);
public override int GetHashCode();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern long GetSystemTimeAsFileTime();
public TypeCode GetTypeCode();
internal bool IsAmbiguousDaylightSavingTime();
public bool IsDaylightSavingTime();
public static bool IsLeapYear(int year);
public static DateTime operator +(DateTime d, TimeSpan t);
public static bool operator ==(DateTime d1, DateTime d2);
public static bool operator >(DateTime t1, DateTime t2);
public static bool operator >=(DateTime t1, DateTime t2);
public static bool operator !=(DateTime d1, DateTime d2);
public static bool operator <(DateTime t1, DateTime t2);
public static bool operator <=(DateTime t1, DateTime t2);
public static TimeSpan operator -(DateTime d1, DateTime d2);
public static DateTime operator -(DateTime d, TimeSpan t);
public static DateTime Parse(string s);
public static DateTime Parse(string s, IFormatProvider provider);
public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles styles);
public static DateTime ParseExact(string s, string format, IFormatProvider provider);
public static DateTime ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style);
public static DateTime ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style);
public static DateTime SpecifyKind(DateTime value, DateTimeKind kind);
public TimeSpan Subtract(DateTime value);
public DateTime Subtract(TimeSpan value);
bool IConvertible.ToBoolean(IFormatProvider provider);
byte IConvertible.ToByte(IFormatProvider provider);
char IConvertible.ToChar(IFormatProvider provider);
DateTime IConvertible.ToDateTime(IFormatProvider provider);
decimal IConvertible.ToDecimal(IFormatProvider provider);
double IConvertible.ToDouble(IFormatProvider provider);
short IConvertible.ToInt16(IFormatProvider provider);
int IConvertible.ToInt32(IFormatProvider provider);
long IConvertible.ToInt64(IFormatProvider provider);
sbyte IConvertible.ToSByte(IFormatProvider provider);
float IConvertible.ToSingle(IFormatProvider provider);
object IConvertible.ToType(Type type, IFormatProvider provider);
ushort IConvertible.ToUInt16(IFormatProvider provider);
uint IConvertible.ToUInt32(IFormatProvider provider);
ulong IConvertible.ToUInt64(IFormatProvider provider);
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context);
private static double TicksToOADate(long value);
private static long TimeToTicks(int hour, int minute, int second);
public long ToBinary();
internal long ToBinaryRaw();
public long ToFileTime();
public long ToFileTimeUtc();
public DateTime ToLocalTime();
public string ToLongDateString();
public string ToLongTimeString();
public double ToOADate();
public string ToShortDateString();
public string ToShortTimeString();
public override string ToString();
public string ToString(IFormatProvider provider);
public string ToString(string format);
public string ToString(string format, IFormatProvider provider);
public DateTime ToUniversalTime();
internal static bool TryCreate(int year, int month, int day, int hour, int minute, int second, int millisecond, out DateTime result);
public static bool TryParse(string s, out DateTime result);
public static bool TryParse(string s, IFormatProvider provider, DateTimeStyles styles, out DateTime result);
public static bool TryParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style, out DateTime result);
public static bool TryParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result);
// Properties
public DateTime Date { get; }
public int Day { get; }
public DayOfWeek DayOfWeek { get; }
public int DayOfYear { get; }
public int Hour { get; }
private ulong InternalKind { get; }
private long InternalTicks { get; }
public DateTimeKind Kind { get; }
public int Millisecond { get; }
public int Minute { get; }
public int Month { get; }
public static DateTime Now { get; }
public int Second { get; }
public long Ticks { get; }
public TimeSpan TimeOfDay { get; }
public static DateTime Today { get; }
public static DateTime UtcNow { get; }
public int Year { get; }
}
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
{
public const long TicksPerMillisecond = 0x2710L;
private const double MillisecondsPerTick = 0.0001;
public const long TicksPerSecond = 0x989680L;
private const double SecondsPerTick = 1E-07;
public const long TicksPerMinute = 0x23c34600L;
private const double MinutesPerTick = 1.6666666666666667E-09;
public const long TicksPerHour = 0x861c46800L;
private const double HoursPerTick = 2.7777777777777777E-11;
public const long TicksPerDay = 0xc92a69c000L;
private const double DaysPerTick = 1.1574074074074074E-12;
private const int MillisPerSecond = 0x3e8;
private const int MillisPerMinute = 0xea60;
private const int MillisPerHour = 0x36ee80;
private const int MillisPerDay = 0x5265c00;
private const long MaxSeconds = 0xd6bf94d5e5L;
private const long MinSeconds = -922337203685L;
private const long MaxMilliSeconds = 0x346dc5d638865L;
private const long MinMilliSeconds = -922337203685477L;
public static readonly TimeSpan Zero;
public static readonly TimeSpan MaxValue;
public static readonly TimeSpan MinValue;
internal long _ticks;
public TimeSpan(long ticks);
public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
public long Ticks { get; }
public int Days { get; }
public int Hours { get; }
public int Milliseconds { get; }
public int Minutes { get; }
public int Seconds { get; }
public double TotalDays { get; }
public double TotalHours { get; }
public double TotalMilliseconds { get; }
public double TotalMinutes { get; }
public double TotalSeconds { get; }
public TimeSpan Add(TimeSpan ts);
public static int Compare(TimeSpan t1, TimeSpan t2);
public int CompareTo(object value);
public int CompareTo(TimeSpan value);
public static TimeSpan FromDays(double value);
public TimeSpan Duration();
public override bool Equals(object value);
public bool Equals(TimeSpan obj);
public static bool Equals(TimeSpan t1, TimeSpan t2);
public override int GetHashCode();
public static TimeSpan FromHours(double value);
private static TimeSpan Interval(double value, int scale);
public static TimeSpan FromMilliseconds(double value);
public static TimeSpan FromMinutes(double value);
public TimeSpan Negate();
public static TimeSpan Parse(string s);
public static bool TryParse(string s, out TimeSpan result);
public static TimeSpan FromSeconds(double value);
public TimeSpan Subtract(TimeSpan ts);
public static TimeSpan FromTicks(long value);
internal static long TimeToTicks(int hour, int minute, int second);
private string IntToString(int n, int digits);
public override string ToString();
public static TimeSpan operator -(TimeSpan t);
public static TimeSpan operator -(TimeSpan t1, TimeSpan t2);
public static TimeSpan operator +(TimeSpan t);
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2);
public static bool operator ==(TimeSpan t1, TimeSpan t2);
public static bool operator !=(TimeSpan t1, TimeSpan t2);
public static bool operator <(TimeSpan t1, TimeSpan t2);
public static bool operator <=(TimeSpan t1, TimeSpan t2);
public static bool operator >(TimeSpan t1, TimeSpan t2);
public static bool operator >=(TimeSpan t1, TimeSpan t2);
static TimeSpan();
// Nested Types
[StructLayout(LayoutKind.Sequential)]
private struct StringParser
{
private string str;
private char ch;
private int pos;
private int len;
private ParseError error;
internal void NextChar();
internal char NextNonDigit();
internal long Parse(string s);
internal bool TryParse(string s, out long value);
internal bool ParseInt(int max, out int i);
internal bool ParseTime(out long time);
internal void SkipBlanks();
// Nested Types
private enum ParseError
{
ArgumentNull = 4,
Format = 1,
Overflow = 2,
OverflowHoursMinutesSeconds = 3
}
}
}
#5
1
I am struggling with this same issue, in that I have a project where I have picosecond resolution timestamps. My source data is in "time_t" format, i.e. second since epoch + picoseconds + UTC Offset.
我正在为同样的问题而挣扎,因为我有一个项目,我有皮秒分辨率的时间戳。我的源数据是“time_t”格式,即秒+皮秒+ UTC偏移量。
The best solution I have found is to work with "decimal seconds since epoch in UTC" as my time format internally, and only use DateTime as a pretty print object, to extract locale/formatting up to 1 s resolution, and then manually manipulate the string to include fractional seconds.
我找到的最好的解决方案是在UTC中使用“从UTC时代开始的十进制秒”作为我的时间格式,并且只使用DateTime作为一个漂亮的打印对象,以提取区域/格式到1的分辨率,然后手工操作字符串,包括分数秒。
#6
0
You could probably roll your own in various ways.
你也可以用不同的方式来滚自己的。
1.) Create a structure using System.Decimal field as the "backing store".
1)。使用系统创建结构。十进制字段作为“后备存储器”。
2.) Create a structure using System.Numerics.BigInteger as backing.
2)。使用System.Numerics创建结构。BigInteger作为支持。
Wrap methods to use System.DateTime for convenient "Parts" (year, month, day,...) Include your own Nanonsecond, Picosecond, Femtosecond, etc., properties and methods.
包装方法使用系统。方便的“零件”(年、月、日、…)包括你自己的纳米秒,皮秒,飞秒等等,属性和方法。
FYI:
仅供参考:
DateTime.MaxValue.Ticks : 3,155,378,975,999,999,999
DateTime.MaxValue。蜱虫:3155378975999999999
Decimal.MaxValue : 79,228,162,514,264,337,593,543,950,335
小数。MaxValue:79228162514264337593543950335
BigInteger : Arbitrarily Large!
BigInteger:任意大!
#1
11
You might be able to use DateTime
after all. DateTime.Ticks
' resolution is 100 nanoseconds. You can set the ticks with DateTime.AddTicks
.
毕竟,您可能可以使用DateTime。DateTime。蜱的分辨率是100纳秒。您可以使用DateTime.AddTicks设置刻度。
#2
10
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.)
看看答案和日期时间。滴答属性,可以从给定的值中计算微秒和纳秒。因此,我将这个扩展方法类组合在一起。(遗憾的是,鉴于其他一些需求,我认为我无法使用它,但其他人可能会发现它有用。)
/// <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));
}
}
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.)
这仍然不会让您在创建时设置微秒或纳秒,但是可以在创建后不久添加它们。它也不能提供比日期时间更好的分辨率(例如,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
Here's hoping this works for someone else!
希望这对别人有用!
#3
4
If I really needed more accuracy than the 100 ns resolution provided by DateTime
, I would consider creating a structure that contains a DateTime
and an integer value:
如果我真的需要比DateTime提供的100 ns分辨率更高的精度,我将考虑创建一个包含DateTime和一个整数值的结构:
public struct HiResDateTime
{
public HiResDateTime(DateTime dateTime, int nanoseconds)
{
if (nanoSeconds < 0 || nanoSeconds > 99)
throw new ArgumentOutOfRangeException(...);
DateTime = dateTime;
Nanoseconds = nanoseconds;
}
... possibly other constructors including one that takes a timestamp parameter
... in the format provided by the instruments.
public DateTime DateTime { get; private set; }
public int Nanoseconds { get; private set; }
... implementation ...
}
Then implement whatever is needed, for example:
然后实现任何需要的东西,例如:
- Comparison (
DateTime
first, thenNanoseconds
) - 比较(首先是时间,然后是纳秒)
-
ToString()
e.g. format DateTime to 100 ns accuracy then append nanoseconds. - ToString()例如,将DateTime格式设置为100ns精确度,然后附加纳秒。
- Conversion to/from
DateTime
- 转换到/从DateTime
- Add/subtract (might need a similar
HiResTimeSpan
) ... etc. ... - 加减(可能需要一个类似的HiResTimeSpan)……等……
#4
2
If you want something that operates on significant fractions of a microsecond, then No. The thing you're asking for doesn't exist as part of the standard libraries, but for what you're asking, why do you need this? It sounds like you really need two components, a string (variable length, hold's almost any conceivable value) and a DateTime for the UTC standard formatted date/time that you get natively.
如果你想要的是对一微秒的重要部分起作用的东西,那就不要。你想要的东西不是作为标准库的一部分存在的,但是你想问的是,为什么你需要这个?听起来您确实需要两个组件,一个字符串(可变长度,hold几乎是任何可能的值)和一个DateTime,用于您本地获得的UTC标准格式化日期/时间。
Micro/nano scale second timekeeping is not in the "normal" range of computations, so it's not provided in the "normal" .NET libraries.
微/纳米级的二次计时不属于“正常”计算范围,所以在“正常”. net库中没有提供。
What will you be doing with these timestamps? Will you be comparing them? Adding/subtracting them? I would suggest running reflector for the basic DateTime object (actually I think I'm gonna do that real quick too)
你打算用这些时间戳做什么?你会比较他们吗?增加/减少他们吗?我建议为基本的DateTime对象运行reflector(实际上我想我也会很快地这么做)
For your benefit, here's the simple version of the .NET Reflector disassembly of the standard DateTime object (and since the other answer at the time of this edit suggests the TimeSpan element, that as well)
为了您的利益,这里是标准DateTime对象的。net Reflector反汇编的简单版本(由于编辑时的另一个答案是TimeSpan元素)
[Serializable]
public struct DateTime : IComparable, IFormattable, IConvertible, ISerializable, IComparable<DateTime>, IEquatable<DateTime>
{
// Fields
private ulong dateData;
private const string DateDataField = "dateData";
private const int DatePartDay = 3;
private const int DatePartDayOfYear = 1;
private const int DatePartMonth = 2;
private const int DatePartYear = 0;
private const int DaysPer100Years = 0x8eac;
private const int DaysPer400Years = 0x23ab1;
private const int DaysPer4Years = 0x5b5;
private const int DaysPerYear = 0x16d;
private const int DaysTo10000 = 0x37b9db;
private const int DaysTo1601 = 0x8eac4;
private const int DaysTo1899 = 0xa9559;
private static readonly int[] DaysToMonth365;
private static readonly int[] DaysToMonth366;
private const long DoubleDateOffset = 0x85103c0cb83c000L;
private const long FileTimeOffset = 0x701ce1722770000L;
private const ulong FlagsMask = 13835058055282163712L;
private const ulong KindLocal = 9223372036854775808L;
private const ulong KindLocalAmbiguousDst = 13835058055282163712L;
private const int KindShift = 0x3e;
private const ulong KindUnspecified = 0L;
private const ulong KindUtc = 0x4000000000000000L;
private const ulong LocalMask = 9223372036854775808L;
private const long MaxMillis = 0x11efae44cb400L;
internal const long MaxTicks = 0x2bca2875f4373fffL;
public static readonly DateTime MaxValue;
private const int MillisPerDay = 0x5265c00;
private const int MillisPerHour = 0x36ee80;
private const int MillisPerMinute = 0xea60;
private const int MillisPerSecond = 0x3e8;
internal const long MinTicks = 0L;
public static readonly DateTime MinValue;
private const double OADateMaxAsDouble = 2958466.0;
private const double OADateMinAsDouble = -657435.0;
private const long OADateMinAsTicks = 0x6efdddaec64000L;
private const long TicksCeiling = 0x4000000000000000L;
private const string TicksField = "ticks";
private const ulong TicksMask = 0x3fffffffffffffffL;
private const long TicksPerDay = 0xc92a69c000L;
private const long TicksPerHour = 0x861c46800L;
private const long TicksPerMillisecond = 0x2710L;
private const long TicksPerMinute = 0x23c34600L;
private const long TicksPerSecond = 0x989680L;
// Methods
static DateTime();
public DateTime(long ticks);
private DateTime(ulong dateData);
public DateTime(long ticks, DateTimeKind kind);
private DateTime(SerializationInfo info, StreamingContext context);
public DateTime(int year, int month, int day);
internal DateTime(long ticks, DateTimeKind kind, bool isAmbiguousDst);
public DateTime(int year, int month, int day, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second);
public DateTime(int year, int month, int day, int hour, int minute, int second, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, DateTimeKind kind);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar);
public DateTime(int year, int month, int day, int hour, int minute, int second, int millisecond, Calendar calendar, DateTimeKind kind);
public DateTime Add(TimeSpan value);
private DateTime Add(double value, int scale);
public DateTime AddDays(double value);
public DateTime AddHours(double value);
public DateTime AddMilliseconds(double value);
public DateTime AddMinutes(double value);
public DateTime AddMonths(int months);
public DateTime AddSeconds(double value);
public DateTime AddTicks(long value);
public DateTime AddYears(int value);
public static int Compare(DateTime t1, DateTime t2);
public int CompareTo(DateTime value);
public int CompareTo(object value);
private static long DateToTicks(int year, int month, int day);
public static int DaysInMonth(int year, int month);
internal static long DoubleDateToTicks(double value);
public bool Equals(DateTime value);
public override bool Equals(object value);
public static bool Equals(DateTime t1, DateTime t2);
public static DateTime FromBinary(long dateData);
internal static DateTime FromBinaryRaw(long dateData);
public static DateTime FromFileTime(long fileTime);
public static DateTime FromFileTimeUtc(long fileTime);
public static DateTime FromOADate(double d);
private int GetDatePart(int part);
public string[] GetDateTimeFormats();
public string[] GetDateTimeFormats(char format);
public string[] GetDateTimeFormats(IFormatProvider provider);
public string[] GetDateTimeFormats(char format, IFormatProvider provider);
public override int GetHashCode();
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern long GetSystemTimeAsFileTime();
public TypeCode GetTypeCode();
internal bool IsAmbiguousDaylightSavingTime();
public bool IsDaylightSavingTime();
public static bool IsLeapYear(int year);
public static DateTime operator +(DateTime d, TimeSpan t);
public static bool operator ==(DateTime d1, DateTime d2);
public static bool operator >(DateTime t1, DateTime t2);
public static bool operator >=(DateTime t1, DateTime t2);
public static bool operator !=(DateTime d1, DateTime d2);
public static bool operator <(DateTime t1, DateTime t2);
public static bool operator <=(DateTime t1, DateTime t2);
public static TimeSpan operator -(DateTime d1, DateTime d2);
public static DateTime operator -(DateTime d, TimeSpan t);
public static DateTime Parse(string s);
public static DateTime Parse(string s, IFormatProvider provider);
public static DateTime Parse(string s, IFormatProvider provider, DateTimeStyles styles);
public static DateTime ParseExact(string s, string format, IFormatProvider provider);
public static DateTime ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style);
public static DateTime ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style);
public static DateTime SpecifyKind(DateTime value, DateTimeKind kind);
public TimeSpan Subtract(DateTime value);
public DateTime Subtract(TimeSpan value);
bool IConvertible.ToBoolean(IFormatProvider provider);
byte IConvertible.ToByte(IFormatProvider provider);
char IConvertible.ToChar(IFormatProvider provider);
DateTime IConvertible.ToDateTime(IFormatProvider provider);
decimal IConvertible.ToDecimal(IFormatProvider provider);
double IConvertible.ToDouble(IFormatProvider provider);
short IConvertible.ToInt16(IFormatProvider provider);
int IConvertible.ToInt32(IFormatProvider provider);
long IConvertible.ToInt64(IFormatProvider provider);
sbyte IConvertible.ToSByte(IFormatProvider provider);
float IConvertible.ToSingle(IFormatProvider provider);
object IConvertible.ToType(Type type, IFormatProvider provider);
ushort IConvertible.ToUInt16(IFormatProvider provider);
uint IConvertible.ToUInt32(IFormatProvider provider);
ulong IConvertible.ToUInt64(IFormatProvider provider);
[SecurityPermission(SecurityAction.LinkDemand, Flags=SecurityPermissionFlag.SerializationFormatter)]
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context);
private static double TicksToOADate(long value);
private static long TimeToTicks(int hour, int minute, int second);
public long ToBinary();
internal long ToBinaryRaw();
public long ToFileTime();
public long ToFileTimeUtc();
public DateTime ToLocalTime();
public string ToLongDateString();
public string ToLongTimeString();
public double ToOADate();
public string ToShortDateString();
public string ToShortTimeString();
public override string ToString();
public string ToString(IFormatProvider provider);
public string ToString(string format);
public string ToString(string format, IFormatProvider provider);
public DateTime ToUniversalTime();
internal static bool TryCreate(int year, int month, int day, int hour, int minute, int second, int millisecond, out DateTime result);
public static bool TryParse(string s, out DateTime result);
public static bool TryParse(string s, IFormatProvider provider, DateTimeStyles styles, out DateTime result);
public static bool TryParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style, out DateTime result);
public static bool TryParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style, out DateTime result);
// Properties
public DateTime Date { get; }
public int Day { get; }
public DayOfWeek DayOfWeek { get; }
public int DayOfYear { get; }
public int Hour { get; }
private ulong InternalKind { get; }
private long InternalTicks { get; }
public DateTimeKind Kind { get; }
public int Millisecond { get; }
public int Minute { get; }
public int Month { get; }
public static DateTime Now { get; }
public int Second { get; }
public long Ticks { get; }
public TimeSpan TimeOfDay { get; }
public static DateTime Today { get; }
public static DateTime UtcNow { get; }
public int Year { get; }
}
[Serializable, StructLayout(LayoutKind.Sequential), ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
{
public const long TicksPerMillisecond = 0x2710L;
private const double MillisecondsPerTick = 0.0001;
public const long TicksPerSecond = 0x989680L;
private const double SecondsPerTick = 1E-07;
public const long TicksPerMinute = 0x23c34600L;
private const double MinutesPerTick = 1.6666666666666667E-09;
public const long TicksPerHour = 0x861c46800L;
private const double HoursPerTick = 2.7777777777777777E-11;
public const long TicksPerDay = 0xc92a69c000L;
private const double DaysPerTick = 1.1574074074074074E-12;
private const int MillisPerSecond = 0x3e8;
private const int MillisPerMinute = 0xea60;
private const int MillisPerHour = 0x36ee80;
private const int MillisPerDay = 0x5265c00;
private const long MaxSeconds = 0xd6bf94d5e5L;
private const long MinSeconds = -922337203685L;
private const long MaxMilliSeconds = 0x346dc5d638865L;
private const long MinMilliSeconds = -922337203685477L;
public static readonly TimeSpan Zero;
public static readonly TimeSpan MaxValue;
public static readonly TimeSpan MinValue;
internal long _ticks;
public TimeSpan(long ticks);
public TimeSpan(int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds);
public TimeSpan(int days, int hours, int minutes, int seconds, int milliseconds);
public long Ticks { get; }
public int Days { get; }
public int Hours { get; }
public int Milliseconds { get; }
public int Minutes { get; }
public int Seconds { get; }
public double TotalDays { get; }
public double TotalHours { get; }
public double TotalMilliseconds { get; }
public double TotalMinutes { get; }
public double TotalSeconds { get; }
public TimeSpan Add(TimeSpan ts);
public static int Compare(TimeSpan t1, TimeSpan t2);
public int CompareTo(object value);
public int CompareTo(TimeSpan value);
public static TimeSpan FromDays(double value);
public TimeSpan Duration();
public override bool Equals(object value);
public bool Equals(TimeSpan obj);
public static bool Equals(TimeSpan t1, TimeSpan t2);
public override int GetHashCode();
public static TimeSpan FromHours(double value);
private static TimeSpan Interval(double value, int scale);
public static TimeSpan FromMilliseconds(double value);
public static TimeSpan FromMinutes(double value);
public TimeSpan Negate();
public static TimeSpan Parse(string s);
public static bool TryParse(string s, out TimeSpan result);
public static TimeSpan FromSeconds(double value);
public TimeSpan Subtract(TimeSpan ts);
public static TimeSpan FromTicks(long value);
internal static long TimeToTicks(int hour, int minute, int second);
private string IntToString(int n, int digits);
public override string ToString();
public static TimeSpan operator -(TimeSpan t);
public static TimeSpan operator -(TimeSpan t1, TimeSpan t2);
public static TimeSpan operator +(TimeSpan t);
public static TimeSpan operator +(TimeSpan t1, TimeSpan t2);
public static bool operator ==(TimeSpan t1, TimeSpan t2);
public static bool operator !=(TimeSpan t1, TimeSpan t2);
public static bool operator <(TimeSpan t1, TimeSpan t2);
public static bool operator <=(TimeSpan t1, TimeSpan t2);
public static bool operator >(TimeSpan t1, TimeSpan t2);
public static bool operator >=(TimeSpan t1, TimeSpan t2);
static TimeSpan();
// Nested Types
[StructLayout(LayoutKind.Sequential)]
private struct StringParser
{
private string str;
private char ch;
private int pos;
private int len;
private ParseError error;
internal void NextChar();
internal char NextNonDigit();
internal long Parse(string s);
internal bool TryParse(string s, out long value);
internal bool ParseInt(int max, out int i);
internal bool ParseTime(out long time);
internal void SkipBlanks();
// Nested Types
private enum ParseError
{
ArgumentNull = 4,
Format = 1,
Overflow = 2,
OverflowHoursMinutesSeconds = 3
}
}
}
#5
1
I am struggling with this same issue, in that I have a project where I have picosecond resolution timestamps. My source data is in "time_t" format, i.e. second since epoch + picoseconds + UTC Offset.
我正在为同样的问题而挣扎,因为我有一个项目,我有皮秒分辨率的时间戳。我的源数据是“time_t”格式,即秒+皮秒+ UTC偏移量。
The best solution I have found is to work with "decimal seconds since epoch in UTC" as my time format internally, and only use DateTime as a pretty print object, to extract locale/formatting up to 1 s resolution, and then manually manipulate the string to include fractional seconds.
我找到的最好的解决方案是在UTC中使用“从UTC时代开始的十进制秒”作为我的时间格式,并且只使用DateTime作为一个漂亮的打印对象,以提取区域/格式到1的分辨率,然后手工操作字符串,包括分数秒。
#6
0
You could probably roll your own in various ways.
你也可以用不同的方式来滚自己的。
1.) Create a structure using System.Decimal field as the "backing store".
1)。使用系统创建结构。十进制字段作为“后备存储器”。
2.) Create a structure using System.Numerics.BigInteger as backing.
2)。使用System.Numerics创建结构。BigInteger作为支持。
Wrap methods to use System.DateTime for convenient "Parts" (year, month, day,...) Include your own Nanonsecond, Picosecond, Femtosecond, etc., properties and methods.
包装方法使用系统。方便的“零件”(年、月、日、…)包括你自己的纳米秒,皮秒,飞秒等等,属性和方法。
FYI:
仅供参考:
DateTime.MaxValue.Ticks : 3,155,378,975,999,999,999
DateTime.MaxValue。蜱虫:3155378975999999999
Decimal.MaxValue : 79,228,162,514,264,337,593,543,950,335
小数。MaxValue:79228162514264337593543950335
BigInteger : Arbitrarily Large!
BigInteger:任意大!