Skip to content

Commit

Permalink
modules/cron: avoid rounding in timespec_to_double
Browse files Browse the repository at this point in the history
Avoid implicit rounding in timespec_to_double in both cron.c and task.c.

For the cron task timestamps, rename the local function
round_timespec_to_double() to be honest about what we're doing,
and expand the comment to explain why.

For other cron timestamps, just drop the rounding for now. If it
an inconvenience in the future (unlikely), the rounding can be
copied from task.c as an example.

Resolves #1131
  • Loading branch information
grondo committed Aug 7, 2017
1 parent c99c16a commit 437a4b1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 1 addition & 8 deletions src/modules/cron/cron.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,11 @@ void *cron_entry_type_data (cron_entry_t *e)
return e->data;
}

static double timespec_to_double (struct timespec *tm)
{
double s = tm->tv_sec;
double ns = tm->tv_nsec/1.0e9 + .5e-9; // round 1/2 epsilon
return (s + ns);
}

double get_timestamp (void)
{
struct timespec tm;
clock_gettime (CLOCK_REALTIME, &tm);
return timespec_to_double (&tm);
return ((double) tm.tv_sec + (tm.tv_nsec/1.0e9));
}

static void timeout_cb (flux_t *h, cron_task_t *t, void *arg)
Expand Down
11 changes: 8 additions & 3 deletions src/modules/cron/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -447,10 +447,15 @@ int cron_task_status (cron_task_t *t)
return (t->status);
}

static double timespec_to_double (struct timespec *tm)
static double round_timespec_to_double (struct timespec *tm)
{
double s = tm->tv_sec;
double ns = tm->tv_nsec/1.0e9 + .5e-9; // round 1/2 epsilon
/* Add .5ns (1/2 the minumim possible value change) to avoid
* underflow which represents something like .5 as .499999...
* (we don't care about overflow since we'll truncate fractional
* part to 9 significant digits *at the most* anyway)
*/
double ns = tm->tv_nsec/1.0e9 + .5e-9;
return (s + ns);
}

Expand Down Expand Up @@ -480,7 +485,7 @@ static const char * cron_task_state_string (cron_task_t *t)
*/
static int add_timespec (json_t *o, const char *name, struct timespec *tm)
{
json_t *n = json_real (timespec_to_double (tm));
json_t *n = json_real (round_timespec_to_double (tm));
if (n == NULL)
return -1;
return json_object_set_new (o, name, n);
Expand Down

0 comments on commit 437a4b1

Please sign in to comment.