34

I'm looking for an equivalent to GetTickCount() on Linux.

Presently I am using Python's time.time() which presumably calls through to gettimeofday(). My concern is that the time returned (the unix epoch), may change erratically if the clock is messed with, such as by NTP. A simple process or system wall time, that only increases positively at a constant rate would suffice.

Does any such time function in C or Python exist?

Giulio Vian
  • 8,248
  • 2
  • 33
  • 41
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
  • related: [How do I get monotonic time durations in python?](http://stackoverflow.com/q/1205722/4279) – jfs Jan 18 '15 at 10:59

4 Answers4

31

You can use CLOCK_MONOTONIC e.g. in C:

struct timespec ts;
if(clock_gettime(CLOCK_MONOTONIC,&ts) != 0) {
 //error
}

See this question for a Python way - How do I get monotonic time durations in python?

Community
  • 1
  • 1
nos
  • 223,662
  • 58
  • 417
  • 506
  • 2
    And on very old Linux systems you can use /proc/uptime as a monotonic time source. Clunky but works. – Zan Lynx Jun 03 '10 at 21:37
  • 4
    There's a note on the manpage: `CLOCK_MONOTONIC_RAW` (since Linux 2.6.28; Linux-specific) Similar to `CLOCK_MONOTONIC`, but provides access to a raw hard‐ ware-based time that is not subject to NTP adjustments. Does this mean that `CLOCK_MONOTONIC_RAW` is more appropriate? – Matt Joiner Jun 04 '10 at 04:20
  • 2
    @MattJoiner: When `CLOCK_MONOTONIC_RAW` talks about NTP adjustments, it is referring to the *rate* of the clock rather than the absolute value. `CLOCK_MONOTONIC` will not skip (as the name implies), and is usually the correct choice. – caf Oct 12 '11 at 04:34
  • 1
    It might be worth checking out this question about CLOCK_MONOTONIC: http://stackoverflow.com/questions/3657289/linux-clock-gettimeclock-monotonic-strange-non-monotonic-behavior – Brett Jul 23 '12 at 02:59
8

This seems to work:

#include <unistd.h>
#include <time.h>

uint32_t getTick() {
    struct timespec ts;
    unsigned theTick = 0U;
    clock_gettime( CLOCK_REALTIME, &ts );
    theTick  = ts.tv_nsec / 1000000;
    theTick += ts.tv_sec * 1000;
    return theTick;
}

yes, get_tick() Is the backbone of my applications. Consisting of one state machine for each 'task' eg, can multi-task without using threads and Inter Process Communication Can implement non-blocking delays.

Wiz
  • 2,145
  • 18
  • 15
6

You should use: clock_gettime(CLOCK_MONOTONIC, &tp);. This call is not affected by the adjustment of the system time just like GetTickCount() on Windows.

macgarden
  • 361
  • 2
  • 2
1

Yes, the kernel has high-resolution timers but it is differently. I would recommend that you look at the sources of any odd project that wraps this in a portable manner.

From C/C++ I usually #ifdef this and use gettimeofday() on Linux which gives me microsecond resolution. I often add this as a fraction to the seconds since epoch I also receive giving me a double.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725