is it possible to get the number of microseconds that passed since 1.1.1970?
I need it to recored input off parallel port.
is it possible to get the number of microseconds that passed since 1.1.1970?
I need it to recored input off parallel port.
Not in a portable fashion. The standard time() function can return the time in any resolution (seconds, microseconds, hours, whatever), and needn't be from the epoch.
You'll have to look at platform-specific functions. See gettimeofday() if you're on a POSIX system.
I'm running ubuntu linux
time_t is the number of seconds since the epoch on ubuntu. With the number of seconds, it is easy to get the microseconds by multiplying that value by 1,000,000. Just be careful about overflowing whatever int type you are using.
I tried this, but this returns "1306944180000000" (it just second times million, not microseconds)
Code:while(1){ time_t current_time = time(0); printf("%ld \n",current_time*1000000); }
There are 1 million microseconds in 1 second. And I do not think time_t can hold that result. You probably need a bigger type.
yes, i know that there are 1 million microseconds in 1 second, I tried to use LONG but this didn't work as well...
It would probably be a better idea to maintain two variables... the number of seconds since the epoch and a separate for the number of microseconds in *this* second.
It can be artificially represented for display with ... printf("%d.%d",seconds,microseconds);
Last edited by CommonTater; 06-01-2011 at 10:34 AM.
Found a solution
Code:#include <sys/time.h> #include <stdio.h> #include <unistd.h> int main() { struct timeval start, end; long mtime, seconds, useconds; gettimeofday(&start, NULL); usleep(2000); gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = seconds + useconds; printf("Elapsed time: %ld microsecons\n", mtime); return 0; }
Last edited by kirill578; 06-01-2011 at 11:21 AM.
Doesn't look right to me...Code:mtime = seconds + useconds;