Nemesis
Nemesis

Reputation: 109

Xamarin TimeSpan use with times smaller than 1 millisecond

I have a Class library (.net Standard 2.0) that uses TimeSpan. If I use the class library in a console application everything works as expected. However when I use it in a Xamarin Forms (Shell App) application, TimeSpan is 00:00:00 for any value smaller than 1 millisecond. My calculations unfortunately require microseconds.

As an example the code below (The actual code is more complex than this, this is just the easiest to show the "error"):

var timetest = TimeSpan.FromSeconds(0.02286 / 943.356);

results in the following for the console application enter image description here and this for the Xamarin application enter image description here

I see that for the console application the values for Days, Hours, Minutes, Seconds and Milliseconds are 32-bit values but for the Xamarin one it is 16-bit.

Is there a way for me to handle the TimeSpan value in Xamarin the same as in the Console Application? Or is it not possible and I have to modify the Class library to just use a Double to represent the seconds?

Upvotes: 1

Views: 229

Answers (1)

Brice Friha
Brice Friha

Reputation: 223

Thanks, Jason for giving the answer in the comments. I'm going to put it here so everyone can clearly see it 😉

Here you create 100 ticks so the equivalent of 0.1 ms:

TimeSpan t = new TimeSpan(100);

So, in this exact context it's:

var timetest = new TimeSpan((int)(TimeSpan.TicksPerSecond*0.02286 / 943.356));

For more about TimeSpan: https://learn.microsoft.com/en-us/dotnet/api/system.timespan.-ctor?view=net-5.0#System_TimeSpan__ctor_System_Int32_System_Int32_System_Int32_

Upvotes: 1

Related Questions