pelib  2.0.0
src/time.c
Go to the documentation of this file.
00001 #include <pelib/time.h>
00002 
00003 #ifdef __cplusplus
00004 extern "C" {
00005 #endif
00006 
00007 const long long int pelib_nsec_in_sec = 1000000000;
00008 
00009 int
00010 pelib_timespec_subtract(struct timespec *result, struct timespec *x, struct timespec *y)
00011 {
00012         /* Perform the carry for the later subtraction by updating y. */
00013         if (x->tv_nsec < y->tv_nsec)
00014         {
00015                 int nsec = (y->tv_nsec - x->tv_nsec) / pelib_nsec_in_sec + 1;
00016                 y->tv_nsec -= pelib_nsec_in_sec * nsec;
00017                 y->tv_sec += nsec;
00018         }
00019         if (x->tv_nsec - y->tv_nsec > pelib_nsec_in_sec)
00020         {
00021                 int nsec = (x->tv_nsec - y->tv_nsec) / pelib_nsec_in_sec;
00022                 y->tv_nsec += pelib_nsec_in_sec * nsec;
00023                 y->tv_sec -= nsec;
00024         }
00025      
00026         /* Compute the time remaining to wait. tv_nsec is certainly positive. */
00027         result->tv_sec = x->tv_sec - y->tv_sec;
00028         result->tv_nsec = x->tv_nsec - y->tv_nsec;
00029      
00030         /* Return 1 if result is negative. */
00031         return x->tv_sec < y->tv_sec;
00032 }
00033 
00034 #ifdef __cplusplus
00035 }
00036 #endif
00037