Timestamp function or class object

1 view (last 30 days)
Hey Guys, I want to send and record the Timestamp when i create a Message. How to send timestamp along with Message via fprintf or fwrite or any other command Can someone write a function or create a class or help me proceed with this.

Accepted Answer

Walter Roberson
Walter Roberson on 21 Jan 2016
You cannot get microseconds when you use datestr and now. The underlying representation is only accurate to less than 10 microseconds for times near this century. If you want to print the guaranteed inaccurate microseconds then you will need to format them yourself.
TheTime = now;
dv = datevec(TheTime);
dv(6) = 0;
reconstructed_time = datenum(dv);
timediff = TheTime - reconstructed_time;
diff_secs = timediff * 24*60*60;
s = sprintf( '%s%09.6f', datestr(dv, 'yyyy-mm-dd HH:MM:'), diff_secs)
The reason for not just grabbing dv(6) and printing it is that in my testing I can see that MATLAB rounds it to the 1000'th of a second.
It is, of course, bad practice to try to create microsecond time stamps of a time that is inherently less than 10 microsecond accuracy. Therefore you should not be using datenum as your time representation. Because if you were using datetime objects like we suggested several days ago, then you would have nanosecond resolution and you would be able to format your objects with up to 9 decimal places of fraction in the time format; see http://www.mathworks.com/help/matlab/ref/datetime-properties.html#prop_Format
Just take your datetime object, assign an appropriate Format property to it, and then char() will format the time appropriately as a string.
  3 Comments
Walter Roberson
Walter Roberson on 21 Jan 2016
microsecond time stamps being the key in that phrase. The older datenum-based now() is a double-precision number which is the whole number of days since a particular event, together with a fraction which is the fraction of a whole day. For example at the time of my writing, "now" is 736350.12410966842435300350189208984375 which is 736350 days since Jan 1 0000, plus a fraction of a day. The uncertainty in floating point representation is eps() of that, which is 0.000000000116415321826934814453125 -- which is 1/8589934592 of a day. That is about 1/99420.5 of a second as the maximum resolution, which is just over 10 microseconds. Floating point numbers would have to be about 4 bits longer than they are now in order to resolve down to 1 microsecond these days.
But this only applies to datenum based time representation. The newer datetime based representation uses more storage internally and can handle down to nanoseconds.
On the other hand, as I indicated in my link in your other closely related question, MATLAB effectively cannot read the clock with microsecond precision. It can get some number, but the time it takes to start fetching the number is variable and untimeable, and the time it takes to return from fetching the number is variable and untimeable, with the uncertainties in the time being in the tens of microsecond range. And depending how you ask for the time, the system itself might not be counting in microseconds or better: the fastest method the hardware has for accessing time information is through registers that are not continuously updated and which do not have the full resolution you want. And the process for accessing the most accurate timing might take a variable amount of time unless you clear out the other activity first.
It is like asking a waiter what time it is. The waiter has a watch to look at, but the waiter's watch is only accurate to the second. The cook has a watch which is accurate to 1/100th of a second, so you ask the waiter to go ask the cook what time it is, But the time it takes the waiter to get to the cook varies according to how busy it is on the way to the kitchen, and on how busy the cook is before the cook has time to answer, and on how busy it is on the way out of the kitchen. "The cook said it was 3:20:43.62". "And exactly how long ago was that?" "I don't know, my own watch is only accurate to the second". No matter how many times you send the waiter back to ask the cook, you cannot figure out what time it actually is "exactly" because the trips all take a different amount of time.
So you have to go outside of MATLAB to get a time reading that is accurate to the microsecond in the first place (go ask the cook). But clearly knowing the exact time an unknown amount of time ago is of limited utility.
Really, it is pointless to try to implement PTP in a language which does not have reproducible timings. Even if you could get a good timestamp, MATLAB is an interpreted language and is fully entitled to spend a few seconds doing "garbage collection" (cleaning up used memory) any time it feels like it. Furthermore, you are trying to do this work on an operating system that has the authority to suspend MATLAB at any time for any reason it feels like, such as running a virus check or defragmenting the disk, or checking for new mail -- or having Clippy show up to say "You seem to be having problems composing a sentence in the document you are writing. Would you like me to show you a thesaurus?"
Stephen23
Stephen23 on 21 Jan 2016
+1 I tip my hat to you, Sir.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!