How to maintain precision with DateTime.Now.Ticks in C #

I know that when I use DateTime.Now.Ticks in C #, it returns a long value, but I need to store it in an int variable, and I'm confused about whether I can maintain this precision. At the moment I just have a throw

int timeStampValue = (int)DateTime.Now.Ticks; 

This is a limitation of the project, so I understand that tons of accuracy are lost. I guess I just couldn't think of another way to make a timestamp stored in int, which I could compare with other timestamps.

Any suggestions or recommendations on how to maintain accuracy, if possible, would be greatly appreciated.

All answers were illustrative. Actually, I just set up a process involving counters, where when the item is used, the counter is set to β€œ0” and all other counters are incremented by 1. Then any one that is the highest counter is the next item to be used.

+7
c # datetime timestamp
source share
5 answers

Do you need all the most important bits? (e.g. what year)

Do you need all the least significant bits? (e.g. sub-nanosecond precision)

How long do you need to measure the interval?

If you only need millisecond precision, why not lose the least significant bits.

 int timeStamp = (int)(DateTime.Now.Ticks >> 10) // lose smallest 10 bits 

change

The OP wants to save time using recently used items: if this is a user choice for a single user, you probably don't want anything shorter than a second! since 10 ^ 7 ticks per second, log (10 ^ 7) / log (2) = 23 redundant bits in a long value!

So how much space do you need? Well, your values ​​should indicate year, month, day, hour, minute and second; In a year, about 32 million seconds = about 24 bits. add 3 bits if you want to keep the last 10 years. Therefore, it easily fits into int32. I suggest

 int timeStamp = (int)(DateTime.Now.Ticks >>23) // retain bits 23 to 55 
+10
source share

DateTime.Now not so accurate.

http://blogs.msdn.com/ericlippert/archive/2010/04/08/precision-and-accuracy-of-datetime.aspx

With that said, if you drop the value to a smaller type, by default you can lose data in any situation.

Consider this conclusion:

 int.MaxValue: 2147483647 DateTime.Now.Ticks: 634075598514933010 

How to do this to implement a realistic option?

+13
source share

If the resolution of the millisecond is good enough, you can save the offsets from some DataTime base into an int field, and then restore the full DateTime when you need it. Milliseconds stored in a 32-bit integer will allow your application to run for 49 days before its completion. Here is a simple helper class that you could use:

 public class TimeOffsetManager { public TimeOffsetManager() { InitialDateTime = DateTime.Now; } public DateTime InitialDateTime { get; private set; } public int GetOffset() { TimeSpan elapsed = DateTime.Now - InitialDateTime; return (int)Math.Round(elapsed.TotalMilliseconds); } public DateTime OffsetToDateTime(int offset) { return InitialDateTime + TimeSpan.FromMilliseconds(offset); } } 

It can be used as:

 public static void Method() { var offsetManager = new TimeOffsetManager(); int offset = offsetManager.GetOffset(); // ... DateTime realTime = offsetManager.OffsetToDateTime(offset); } 
+2
source share

More for kicks than anything ... If you are dead using ints to hold DateTime and maintain accuracy, you can define your own structure containing two ints, each of which contains 4 bytes, and DateTime, which share these bytes.

 public class Program { public static void Main(string[] args) { DateTime now = DateTime.Now; var myDate = new Int32BackedDate(now.Ticks); Console.WriteLine(now); Console.WriteLine(myDate.Date); } } [StructLayout(LayoutKind.Explicit, Size = 8)] public struct Int32BackedDate { [FieldOffset(4)] private readonly int _high; [FieldOffset(0)] private readonly int _low; [FieldOffset(0)] private readonly DateTime _date; public DateTime Date { get { return _date; } } public Int32BackedDate(long ticks) { _date = default(DateTime); byte[] bytes = BitConverter.GetBytes(ticks); _low = BitConverter.ToInt32(bytes, 0); _high = BitConverter.ToInt32(bytes, 4); } } 
+2
source share

Regarding my comment above. What is the accuracy limit? If the limitation is that you cannot store data in a 64-bit variable, then how about doing something line by line.

 uint tsLow = (uint)DateTime.Now.Ticks; uint tsHigh = (uint)(DateTime.Now.Ticks >> 32); 

Now you can store both tsLow and tsHigh for your external data. You can also implement special functions that could calculate values ​​using two separate 32-bit numbers to do your own 64-bit math.

It really depends on your actual limitations. Knowing this, you can offer the best solutions.

+1
source share

All Articles