From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to display time with millisecond accuracy?

The time function only gives second accuracy:

time(&present_time);
converted_time = localtime(&present_time);
strftime(buf, TS_ARRAY_LEN(buf), "%H:%M:%S ", converted_time);

When I tried to use the following from MSVC, CVI did not like the calls.

ts_char timeStr[1024];
struct __timeb64 tstruct;
struct tm *today;
__time64_t ltime;
today = _localtime64( &ltime );
_ftime64( &tstruct );
/* Prefix the entry with a timestamp. */
strftime( timeStr, 128,"%H:%M:%S", today );
sprintf(buf,"%s.%03d ", timeStr, tstruct.millitm);
present_time = (time_t)tstruct.time;

It sounds like these MSVC calls are not ANSI-C compliant, but MS specific. Is there
another ANSI-C way to do this?
0 Kudos
Message 1 of 7
(4,112 Views)
Hello

You might want to try using the Windows SDK for this. You will have to install the SDK support is you havnt already. Check out the GetSystemTime function. This should give you millisecond accuracy according to the documentation.

Make sure to include the window.h header as the very first header in the list of includes.

I hope this helps

Bilal Durrani
NI
Bilal Durrani
NI
Message 2 of 7
(4,112 Views)
The CVI6 prototype for GetSystemTime() is different from Windows SDK prototype.

The CVI6 prototype is
int GetSystemTime(int *hours, int *minutes, int *seconds);
instead of Windows SDK prototype:
VOID GetSystemTime(
LPSYSTEMTIME lpSystemTime // system time
);
0 Kudos
Message 3 of 7
(4,112 Views)
Yup, which is why you need to add the Windows.h header at the very top of your include list. Check out the documentation in the GetSystemTime() fp in CVI. it mentions this. You would want to use the windows version, not the CVI one.


Bilal
Bilal Durrani
NI
0 Kudos
Message 4 of 7
(4,112 Views)
My include structure looks like this, but I still got the error. What am I doing wrong?

/****************************************************************************/
/*-------------------------- WINDOWS DECLARATIONS AND ROUTINES -------------*/
/****************************************************************************/
#include
#include
#include
#include
/****************************************************************************/
/*-------------------------- INCLUDES --------------------------------------*/
/****************************************************************************/
#include /* Needed if linking in external compiler; harmless otherwise */
#
include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#include
#include
#include "mts_lib.h"
#include "mts_comm.h"
#include "mts_msg.h"
#include "mts_power.h"
#include "comm.h"
#include "support.h"
#include "mts_seeker.h"
#include "test_code.h"
#include "time.h"
#include "gps.h"
#include "data_download.h"
0 Kudos
Message 5 of 7
(4,112 Views)
I see the problem

The utlity.h actaully refines the function in windows.h. There are 2 options. You can either not include the utlity.h header.

Or if you need functions from utility.h, you can add the defines that you have for that function in windows.h, after undefining the function that utility.h provided.

So add this before your main( or bore you use GetSystemTime()

#undef GetSystemTime

WINBASEAPI
VOID
WINAPI
GetSystemTime(
OUT LPSYSTEMTIME lpSystemTime
);

This will force the use of the function in the windows SDK, and not CVI. You can do this with any SDK function that has been redfined in another header.

I hope this helps

Bilal
Bilal Durrani
NI
0 Kudos
Message 6 of 7
(4,112 Views)
This works great! I was able to get millisecond accuracy using your recommendation!

Are there other CVI functions that differ from MSVC functions?
0 Kudos
Message 7 of 7
(4,112 Views)