LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

localtime() returns wrong time structure information

I am using the following commands to get time/date information using Labwindows CVI vers.7.0
l_time=localtime(&date);
timestr=asctime(l_time);
where:
time_t date;
char *timestr;
struct tm *l_time;

These commands run on two different computers give the following results:

timestr="Thurs Feb 07 01:28:16 2036"
timestr="Sun Dec 23 12:33:24 1900"

Those commands were executed on Feb 22nd at 9:01pm in 2005!!

I checked the time setting in the control panel on both computers and it was correct.

What could be causing these erronous time/date readings in my labwindows application?

I appreciate your help,
Regards
0 Kudos
Message 1 of 4
(5,969 Views)
Hello Loubna,

First you would need to get the current calendar time, then pass this time to the localtime function, which will adjust the time according to your time zone.

You can do this by making a call to the time() function before you call the localtime() function.

You code would look like:
time (&date);
l_time=localtime(&date);
timestr=asctime(l_time);

Good luck.
Wendy L
LabWindows/CVI Developer Newsletter
Message 2 of 4
(5,961 Views)
Hi loubna.

There are a couple of things to keep in mind, when using the "ansi_c" time functions in CVI:

1. The epoch (the date corresponding to a time_t value of 0) used in CVI is 01 January 1900, not 01 January 1970 as used in most environments (including Unix and C++), so you will probably see discrepancies if you exchange time_t values with other processes or machines.
2. The time_t value you get from the time() function in CVI depends on your current time zone!!! It should be expressed as GMT or UTC (seconds since epoch at the Greenwich meridian), but here in Melbourne, Australia, the result is 3600 seconds (10 hours) TOO BIG. This caused me much grief a while ago, and I switched to using the Windows SDK for time operations.

As far as I know, these issues have not been addressed in CVI 7.x.

Regards,
Colin.
Message 3 of 4
(5,954 Views)
Thank you very much Wendy for the solution.
I tried that yesterday while I was looking at some example codes on the discussion forum.
It works!!just had to add time() and the results were OK.

And Thank you Colin as well for the additional information. I will probably keep the ansi_C commands for now.
But if I see a discrepancy I will switch to Windows SDK commands.
0 Kudos
Message 4 of 4
(5,941 Views)