Name

clock_gettime, clock_settime, clock_getres, clock_getcpuclockid
- clock and time functions

Library

libc.lib

Synopsis

  #include <sys/time.h>
  int clock_gettime (clockid_t clock_id, struct timespec *tp);
  int clock_settime (clockid_t clock_id, const struct timespec *tp);
  int clock_getres (clockid_t clock_id, struct timespec *tp);
  int clock_getcpuclockid (pid_t pid, clockid_t* clock_id);

Return values

All the above APIs return 0 on success and -1 on failure.

Detailed description

The clock_gettime and clock_settime allow the calling process to retrieve or set the value used by a clock which is specified by clock_id.

The clock_id argument can be one of four values: CLOCK_REALTIME for time that increments as a wall clock should, CLOCK_MONOTONIC which increments in SI seconds, CLOCK_VIRTUAL for time that increments only when the CPU is running in user mode on behalf of the calling process, or CLOCK_PROF for time that increments when the CPU is running in user or kernel mode. As of now Symbian OS exposes only wall clock time at user level,hence only CLOCK_REALTIME for all the clock-based APIs is supported.

The structure pointed to by tp is defined in

  #include <sys/time.h>as:

struct timespec {
        time_t  tv_sec;         /* seconds */
        long    tv_nsec;        /* and nanoseconds */
};

                     

The resolution (granularity) of a clock is returned by the clock_getres system call. This value is placed in a (non-NULL) *tp.

The clock ID of the CPU-time clock of the process specified by pid is returned by the clock_getcpuclockid system call. If pid is zero,the clock ID of the CPU-time clock of the process making the call, is returned in. *clock_id.


Examples

#include <time.h>
#include <stdio.h>
int clock_user()
{
        struct timespec tp;
        int retval;
        clockid_t clockid;
        clock_getres (CLOCK_REALTIME, &tp); //Call clock_getres
        printf ("Real time-clock resolution is %d seconds and %d nanoseconds\n", tp.tv_sec, tp.tv_nsec);
        clock_getcpuclockid (0 ,&clockid); //Call clock_getcpuclockid with pid = 0
        printf ("The clock id for the current process is %d\n", clockid);
        tp.tv_sec = 0;
        tp.tv_nsec = 100;
        retval = clock_settime (CLOCK_REALTIME, &tp); //Call clock_settime with 100ns
        printf ("clock_settime returned %d\n", retval);
        clock_gettime (CLOCK_REALTIME, &tp); //Call clock_gettime to fill tp
        printf ("Time from real time-clock is %d seconds and %d nanoseconds\n", tp.tv_sec, tp.tv_nsec);
        return 0;
}

         

Output

Real time-clock resolution is 0 seconds and 1000000 nanoseconds
The clock id for the current process is 0
clock_settime returned 0
Time from real time-clock is 0 seconds and 70663000 nanoseconds

         

Errors

The following error codes may be set in errno:
[EINVAL]
  The clock_id argument was not a valid value.
[EFAULT]
  The *tp argument address referenced invalid memory.

See also

adjtime, ctime

Feedback

For additional information or queries on this page send feedback

© 2008-2009 Nokia Corporation. All rights reserved. This documentation can be used in the connection with this Product to help and support the user.

Top