Get UTC Time in Generated Code

조회 수: 15 (최근 30일)
Michael
Michael 2023년 8월 14일
답변: Corey Silva 2023년 9월 19일
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
Rik
Rik 2023년 8월 15일
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 2023년 9월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Corey Silva
Corey Silva 2023년 9월 19일
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.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by