A few small time utilities written in C
C Makefile
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This is a small collection of small POSIX-specific C programs that deal with time.

POSIX systems represent the current time as the number of seconds since 1970-01-01 00:00:00 UTC, a time referred to as "the epoch".

The displayed precision for any of these programs does not imply that it's actually that accurate. The time to run the program and display the output is likely to exceed one microsecond.

gettimeofday is a simple wrapper around the gettimeofday function. It prints the current time, in seconds since the epoch, to microsecond precision.

clock_gettime is a wrapper around the clock_gettime function. With no arguments, it shows the result of calling clock_gettime, called with CLOCK_REALTIME; this is similar to gettimeofday, but it prints the current time to nanosecond precision. Other options are as shown with -h:

Usage: clock_gettime [options]
    -r CLOCK_REALTIME, system-wide real-time clock
    -m CLOCK_MONOTONIC, monotonic time since some unspecified starting point
    -M CLOCK_MONOTONIC_RAW, hardware-based timer with no ntp adjustments
    -p CLOCK_PROCESS_CPUTIME_ID, high-resolution per-process timer from the CPU
    -t CLOCK_THREAD_CPUTIME_ID, thread-specific CPU-time clock
(-p and -t are not particularly meaningful in this context.)
Multiple arguments may be given
With no arguments, show CLOCK_REALTIME

jd uses the gettimeofday function, but it displays the current Julian date. jd -m prints the Modified Julian Date. The value is printed to 9 decimal places, which gives a precision of 1 billionth of a day or 86.4 microseconds. (This doesn't imply accuracy to that level.)

timer is more complex. timer -h shows the following help message:

Usage: ./timer [options]
    -a : Absolute time (default is zero-based)
    -r : Show time interval relative to previous line
    -b : Buffered output (default is unbuffered)
    -h : Help (show this message and exit)
    -z : Zero-based time (redundant, this is the default)
    -u : Unbuffered output (redundant, this is the default)
    -o file : Print output to specified file

It reads from stdin and writes to stdout, prepending a timestamp to each line. Like the time command, it lets you see how long a progmam takes to execute, but unlike time it shows timing information for each line of ouput (and doesn't show CPU time).