[Swan-dev] new delta time type

D. Hugh Redelmeier hugh at mimosa.com
Wed Jan 28 10:01:53 EET 2015


| From: D. Hugh Redelmeier <hugh at mimosa.com>

| The new libevent code deals with times in milliseconds.

| It would be good if there were a new type to represent these values.
| Currently unsigned long is used (mostly).

Here's a stopgap type to do the job.  It has the advantage that almost
no logic changes.  It should be a trivial change.  It makes the code's
intent clearer.  It creates an abstraction.

The disadvantage is that C won't catch any more errors with this.

/* name not well-considered */

/*
 * precise_delta_t: delta-time with sub-second precision
 *
 * - currently milliseconds (this abstraction can be changed)
 * - unlike time_t, it cannot be negative or undefined
 * - does not have range of time_t so it must be used only
 *   for near-term events (less than 50 days on a 32-bit machine)
 */
typedef unsigned long precise_delta_t;
#define precise_delta_scale	1000LU	/* milliseconds / second */
#define precise_delta_fmt "%lu.03%lus"
#define precise_delta_fmt_args(t) (t) / precise_delta_scale, (t) % precise_delta_scale

I haven't looked at what conversion functions are needed.  They should
check that the range isn't exceeded.

to print one of these values:
	printf("value is " precise_delta_fmt "\n",
		precise_delta_fmt_args(deadline));
		

This type should be used for any variable that is to hold one of these
values.  That seems pretty simple.


More information about the Swan-dev mailing list