8 time_t real_seconds(void) {
10 int s = clock_gettime(CLOCK_REALTIME, &t);
15 /* Subtract the `struct timeval' values X and Y,
16 storing the result in RESULT.
17 Return 1 if the difference is negative, otherwise 0. */
18 int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y) {
19 /* Perform the carry for the later subtraction by updating y. */
20 if (x->tv_usec < y->tv_usec) {
21 int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
22 y->tv_usec -= 1000000 * nsec;
25 if (x->tv_usec - y->tv_usec > 1000000) {
26 int nsec = (x->tv_usec - y->tv_usec) / 1000000;
27 y->tv_usec += 1000000 * nsec;
31 /* Compute the time remaining to wait.
32 tv_usec is certainly positive. */
33 result->tv_sec = x->tv_sec - y->tv_sec;
34 result->tv_usec = x->tv_usec - y->tv_usec;
36 /* Return 1 if result is negative. */
37 return x->tv_sec < y->tv_sec;
40 void sleep_until(time_t dst_sec) {
44 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &dst, NULL)) /* nothing */;
47 time_t round_up(time_t t, int align) {
48 t = t - 1; /* counter bad +t for aligned things in last step */
49 t = t - t%align; /* round down to align */
50 t = t + align; /* add alignment to make it a round up */