Warning: main(/www/www/htdocs/style/globals.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 1
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/globals.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 1
Warning: main(/www/www/htdocs/style/header.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 8
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/header.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 8
Get the current time of a clock
#include <time.h>
int clock_gettime( clockid_t clock_id,
struct timespec * tp );
- clock_id
- The ID of the clock whose time you want to get.
- tp
- A pointer to a
timespec
structure where clock_gettime() can store the time.
This function sets the members as follows:
- tv_sec — the number of seconds since 1970.
- tv_nsec — the number of nanoseconds expired in the
current second.
This value increases by some multiple of nanoseconds, based on the
system clock's resolution.
libc
Use the -l c option to
qcc
to link against this library.
This library is usually included automatically.
The clock_gettime() function gets the current time of the clock specified by clock_id,
and puts it into the buffer pointed to by tp.
- 0
- Success.
- -1
- An error occurred (errno is set).
- EFAULT
- A fault occurred trying to access the buffers provided.
- EINVAL
- Invalid clock_id.
- ESRCH
- The process associated with this request doesn't exist.
/*
* This program calculates the time required to
* execute the program specified as its first argument.
* The time is printed in seconds, on standard out.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
#define BILLION 1000000000L;
int main( int argc, char** argv )
{
struct timespec start, stop;
double accum;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
return EXIT_FAILURE;
}
system( argv[1] );
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
return EXIT_FAILURE;
}
accum = ( stop.tv_sec - start.tv_sec )
+ (double)( stop.tv_nsec - start.tv_nsec )
/ (double)BILLION;
printf( "%lf\n", accum );
return EXIT_SUCCESS;
}
POSIX 1003.1 TMR
Safety: | |
Cancellation point |
No |
Interrupt handler |
No |
Signal handler |
Yes |
Thread |
Yes |
clock_getres(),
clock_settime(),
errno,
timespec
Clocks, Timers, and Getting a Kick Every So Often
chapter of Getting Started with QNX Neutrino
Warning: main(/www/www/htdocs/style/footer.php) [function.main]: failed to open stream: No such file or directory in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 205
Warning: main() [function.include]: Failed opening '/www/www/htdocs/style/footer.php' for inclusion (include_path='.:/www/www/common:/www/www/php/lib/php') in /www/www/docs/6.4.1/neutrino/lib_ref/c/clock_gettime.html on line 205