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.
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?
You might be able to use
DateTime
after all.DateTime.Ticks
' resolution is 100 nanoseconds. You can set the ticks withDateTime.AddTicks
.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.
Micro/nano scale second timekeeping is not in the "normal" range of computations, so it's not provided in the "normal" .NET libraries.
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)
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)
You could probably roll your own in various ways.
1.) Create a structure using System.Decimal field as the "backing store".
2.) Create a structure using System.Numerics.BigInteger as backing.
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
Decimal.MaxValue : 79,228,162,514,264,337,593,543,950,335
BigInteger : Arbitrarily Large!
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.)
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.)
Here's hoping this works for someone else!
If I really needed more accuracy than the 100 ns resolution provided by
DateTime
, I would consider creating a structure that contains aDateTime
and an integer value:Then implement whatever is needed, for example:
DateTime
first, thenNanoseconds
)ToString()
e.g. format DateTime to 100 ns accuracy then append nanoseconds.DateTime
HiResTimeSpan
) ... etc. ...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.
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.