Project

General

Profile

How to get system time?

Added by Mary Frantz over 11 years ago

How do you get the system time in order to timestamp events?

Tried clock_gettime() but got a linker error: "undefined reference to `clock_gettime' "
There was also no reference for CLOCK_REALTIME, or CLOCK_MONOTONIC.

I would like 1 msec resolution or finer.


Replies (10)

RE: How to get system time? - Added by Michael Williamson over 11 years ago

Did you include <time.h> in your code?

RE: How to get system time? - Added by Dominic Giambo over 11 years ago

Hi Mary,

Be sure you are linking to the rt library which implements that call (as well as including the time.h header). Use -lrt as a linker argument to do this.

RE: How to get system time? - Added by Mary Frantz over 11 years ago

Thanks,
I added rt to the project Properties->C/C++ Build -> Settings -> Cross G++ Linker -> Libraries

I had already included time.h

It compiled if I set the first parameter of clock_gettime() to 0, but I still can't reference CLOCK_MONOTONIC.

    timespec time;

    if (clock_gettime(CLOCK_MONOTONIC, &time) < 0)
             error(0, errno,"ERROR_clearLog called clock_gettime():");
<pre/>

I get the message: "Symbol 'CLOCK_MONOTONIC' could not be resolved" 
I tried right clicking the project then Index -> Rebuild, no luck

What kind of time am I getting with clock_gettime(0, &time) ?

RE: How to get system time? - Added by Dominic Giambo over 11 years ago

Not sure why it's not finding CLOCK_MONOTONIC, it's defined in time.h:

45  * The IDs of the various system clocks (for POSIX.1b interval timers):
46 */
47 #define CLOCK_REALTIME 0
48 #define CLOCK_MONOTONIC 1
49 #define CLOCK_PROCESS_CPUTIME_ID 2
50 #define CLOCK_THREAD_CPUTIME_ID 3
51 #define CLOCK_MONOTONIC_RAW 4
52 #define CLOCK_REALTIME_COARSE 5
53 #define CLOCK_MONOTONIC_COARSE 6
54 #define CLOCK_BOOTTIME 7
55 #define CLOCK_REALTIME_ALARM 8
56 #define CLOCK_BOOTTIME_ALARM 9

Looks like with zero you are using CLOCK_REALTIME.

You might want to check the include and linker directories to make certain they are referencing the correct files for the target.

RE: How to get system time? - Added by Mary Frantz over 11 years ago

Where is the correct time.h file? There seem to be alot of them in various include directories.

#include <time.h>
if I right click then select "Open Declaration" it opens:
/usr/local/oeore-i686/sysroots/armv5te-angstrom-linux-gnueabi/usr/include/time.h

which does not contain a definition for CLOCK_MONOTONIC but does have a prototype for clock_gettime()

#include <sys/time.h>
if I right click then select "Open Declaration" it opens:
/usr/local/oeore-i686/sysroots/armv5te-angstrom-linux-gnueabi/usr/include/sys/time.h
which does not have either clock_gettime() or CLOCK_MONOTONIC

Dominic Giambo said:

You might want to check the include and linker directories to make certain they are referencing the correct files for the target.

What should they be?

RE: How to get system time? - Added by Jonathan Cormier over 11 years ago

Looks like you want linux/time.h located at /usr/local/oeore-i686/sysroots/armv5te-angstrom-linux-gnueabi/usr/include/linux/time.h

RE: How to get system time? - Added by Jonathan Cormier over 11 years ago

At least that file has the CLOCK_MONOTONIC in it but doesn't have clock_gettime...

RE: How to get system time? - Added by Jonathan Cormier over 11 years ago

This stack overflow article seems to point to an eclipse bug and possible fix.
http://stackoverflow.com/questions/8881617/constants-not-loaded-by-compiler

RE: How to get system time? - Added by Mary Frantz over 11 years ago

Stumbled across this:

#include <time.h> references /usr/local/oeore-i686/sysroots/armv5te-angstrom-linux-gnueabi/usr/include/time.h

which includes another file:
#include <bits/time.h>
which references
/usr/include/bits/time.h

within /usr/include/bits/time.h

#  ifdef __USE_POSIX199309
/* Identifier for system-wide realtime clock.  */
#   define CLOCK_REALTIME        0
/* Monotonic system-wide clock.  */
#   define CLOCK_MONOTONIC        1
/* High-resolution timer from the CPU.  */
#   define CLOCK_PROCESS_CPUTIME_ID    2
/* Thread-specific CPU-time clock.  */
#   define CLOCK_THREAD_CPUTIME_ID    3

/* Flag to indicate time is absolute.  */
#   define TIMER_ABSTIME        1
#  endif

In the project properties -> C/C++ Build -> Settings -> Cross G++ Compiler -> Preprocessor
I added __USE_POSIX199309

In code:

if (clock_gettime(CLOCK_MONOTONIC, &time) < 0)
        error(0, errno,"ERROR_clearLog called clock_gettime():");

This now compiles, but I still have a marker in the file telling me that CLOCK_MONOTONIC is unresolved.
Yes, I tried Index->Rebuild with no change.

Indexer preferences:
Files to index up-front:
cstdarg, stdarg.h, stddef.h, sys/resource.h, ctime, time.h, sys/types.h, signal.h, cstdio

    (1-10/10)
    Go to top
    Add picture from clipboard (Maximum size: 1 GB)