Get UTC Time in Generated Code

I need to get UTC time down to miliseconds at least (i.e. 1692039433503685.321 seconds) in a piece of generated code C++ code. Unfortunately, Coder doesn't support the 'TimeZone' inputs in datetime, otherwise I'd just use posixtime( datetime('now','TimeZone','UTC') ).
I tried using coder.ceval to call the fuctions in the chono C++ library, but the UTC clock there is only available in the C++20 language standard and Matlab only supports up to C++11.
Is there a good way of doing this?

4 comentarios

James Tursa
James Tursa el 15 de Ag. de 2023
Editada: James Tursa el 15 de Ag. de 2023
Does the Coder allow the now( ) function? What exactly are you looking for? UTC or posixtime? And if you are looking for UTC time what format (date or seconds since some date etc.) are you looking for? How is your generated code going to know about your local time zone?
Michael
Michael el 15 de Ag. de 2023
Coder does not support now.m. The now.m documentation points you to datetime.m. That function allows for datetime('now') as an input, but the input arguments for specifying the timezone ('UTC') is not supported for code generation. I need elapsed seconds since 00:00:000.000 Jan 1 1970 i.e. posix time in the UTC time zone.
The question about how my generated code is going to know about my timezone is a good one. I don't know enough about system clocks to know how my computer knows what timezone it is in (via the network I presume), but it seems to have that information. Other languages (C++ for example) have libraries that include a UTC clock, so I thought it was conceivable that it could be done in Matlab.
Rik
Rik el 15 de Ag. de 2023
There is a UTC in C. Can you use that?
My getUTC function doesn't support code generation, but it does include this C function:
utc_time_c={'#include "mex.h"';
'#include "time.h"';
'';
'/* Abraham Cohn, 3/17/2005 */';
'/* Philips Medical Systems */';
'';
'void mexFunction(int nlhs, mxArray *plhs[], int nrhs,';
' const mxArray *prhs[])';
'{';
' time_t utc;';
' ';
' if (nlhs > 1) {';
' mexErrMsgTxt("Too many output arguments");';
' }';
' ';
' /* Here is a nice ref: www.cplusplus.com/ref/ctime/time.html */';
' time(&utc);';
' /* mexPrintf("UTC time in local zone: %s",ctime(&utc)); */';
' /* mexPrintf("UTC time in GMT: %s",asctime(gmtime(&utc))); */';
' ';
' /* Create matrix for the return argument. */';
' plhs[0] = mxCreateDoubleScalar((double)utc);';
' ';
'}'};
Denis Gurchenkov
Denis Gurchenkov el 18 de Sept. de 2023
Hi Mike, I don't have an answer to the main questipn you have, but one possible avenue for progress is to use the C++ 20 functions.
You said that "MATLAB Coder only supports up to C++11" - that is not quite correct. Coder-generated code should work just fine with C++20. The page you referred to just says "coder does not know how to use the C++20 features - at most, it'll generate code using C++11 features". But since C++ standards are generally forward compatible, you should be able to just set the language flag for the C++ compiler. If you add the following line to your matlab function:
coder.updateBuildInfo('addCompileFlags','/std:c++20');
and generate code using "codegen ... -lang:C++" then you should /std:c++20 added to the c compiler command line.

Iniciar sesión para comentar.

Respuestas (1)

Corey Silva
Corey Silva el 19 de Sept. de 2023

0 votos

Hi Michael,
You should be all set to use posixtime here with an unzoned datetime.
From the posixtime help text:
"If T is unzoned, then POSIXTIME treats T as though its time zone is UTC, and not your local time zone."
This means that posixtime(datetime('now','TimeZone','UTC')) and posixtime(datetime('now')) have the same output.

Categorías

Productos

Versión

R2023a

Preguntada:

el 14 de Ag. de 2023

Respondida:

el 19 de Sept. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by