Current system time as a Timestamp

481 visualizaciones (últimos 30 días)
PTP
PTP el 20 de En. de 2016
Comentada: Walter Roberson el 18 de Mzo. de 2020
Hey,
I want to get the current system time in microseconds and share this timestamp with other computer via UDP. How to do that. fprintf('This message is sent at time %f', time) Is something like above possible in matlab? Please help me with this.

Respuesta aceptada

Robert
Robert el 20 de En. de 2016
Try
help now
for details on MATLAB's function now that returns the current time as a DateNum. You might also be interested in clock or cputime.

Más respuestas (4)

Ilham Hardy
Ilham Hardy el 20 de En. de 2016
fprintf('This message is sent at time %s\n', datestr(now,'HH:MM:SS.FFF'))
  2 comentarios
PTP
PTP el 20 de En. de 2016
Hi Hardy,
Can i also get microseconds with this. It would be really nice if I could achieve that. I tried this fprintf('This message is sent at time %s\n', datestr(now,'HH:MM:SS.FFFFFF')) but shows errors.
Thanks
Walter Roberson
Walter Roberson el 21 de En. de 2016
No, 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.

Iniciar sesión para comentar.


Jan
Jan el 21 de En. de 2016
A timestamp with a precision of microseconds is not meaningful, when the command to obtain the timestamp needs more than a microsecond. Then it is not exactly defined if the stamp belongs to the time before or after the command.

Walter Roberson
Walter Roberson el 21 de En. de 2016
No, this is not implemented by MATLAB. It is difficult to do, and the techniques are operating-system dependent.

Peter Perkins
Peter Perkins el 25 de Mzo. de 2019
This is an old post the got reopened. I feel compelled to say that in anything like recent versions of MATLAB, datetime('now') is much preferred over now.
On Windows, the clock function and datetime('now') both return a system time rounded to milliseconds. On Linux or Mac, it's rounded to microseconds. Whether or not either of those is actually accurate or not depends entirely on your system though.
  2 comentarios
Haoran WANG
Haoran WANG el 15 de Mzo. de 2020
Thank you for your hint. But how get microseconds system time in matlab in windows?
Walter Roberson
Walter Roberson el 18 de Mzo. de 2020
Haoran WANG : are you looking for clock time down to microsecond precision, or are you looking to create relative timings down to microsecond precision?
Getting the system clock down to microsecond is not easy. Microsoft says that for microseconds you should be querying the performance counters instead, but those are for relative times not for clock times.

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by