00001 #include <sys/time.h> 00002 #include <time.h> 00003 00005 static inline void timeval_add(struct timeval *dst_ts, const struct timeval *src1_ts, const struct timeval *src2_ts) { 00006 dst_ts->tv_sec = src1_ts->tv_sec + src2_ts->tv_sec; 00007 dst_ts->tv_usec = src1_ts->tv_usec + src2_ts->tv_usec; 00008 long int delta_us = dst_ts->tv_usec - 1000000lu; 00009 if (delta_us >= 0) { 00010 dst_ts->tv_usec = delta_us; 00011 dst_ts->tv_sec++; 00012 } 00013 } 00014 00019 static inline void timeval_sub(struct timeval *dst_ts, const struct timeval *src1_ts, const struct timeval *src2_ts) { 00020 dst_ts->tv_sec = src1_ts->tv_sec - src2_ts->tv_sec; 00021 dst_ts->tv_usec = src1_ts->tv_usec - src2_ts->tv_usec; 00022 if (dst_ts->tv_usec < 0) { 00023 dst_ts->tv_usec = dst_ts->tv_usec + 1000000lu; 00024 dst_ts->tv_sec--; 00025 } 00026 } 00027 00028 static inline long timeval_sub_us(const struct timeval *src1_tv, const struct timeval *src2_tv) { 00029 return (src1_tv->tv_sec - src2_tv->tv_sec) * 1000000L 00030 + (src1_tv->tv_usec - src2_tv->tv_usec); 00031 } 00032 00033 static inline int timeval_lt(const struct timeval *t1, const struct timeval *t2) { 00034 return (t1->tv_sec < t2->tv_sec) 00035 || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec < t2->tv_usec)); 00036 } 00037 00038 static inline int timeval_le(const struct timeval *t1, const struct timeval *t2) { 00039 return (t1->tv_sec < t2->tv_sec) 00040 || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)); 00041 } 00042 00043 static inline int timeval_eq(const struct timeval *t1, const struct timeval *t2) { 00044 return (t1->tv_sec == t2->tv_sec) && (t1->tv_usec == t2->tv_usec); 00045 }