glong flx_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
g_assert(a);
g_assert(b);
- g_assert(flx_timeval_compare(a, b) >= 0);
- return (a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
+ if (flx_timeval_compare(a, b) < 0)
+ return flx_timeval_diff(b, a);
+
+ return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
}
return 0;
}
+
+GTimeVal *flx_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
+ g_assert(tv);
+
+ g_get_current_time(tv);
+
+ if (msec)
+ g_time_val_add(tv, msec*1000);
+
+ if (jitter)
+ g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
+
+ return tv;
+}
+
+gint flx_age(const GTimeVal *a) {
+ GTimeVal now;
+
+ g_assert(a);
+
+ g_get_current_time(&now);
+
+ return flx_timeval_diff(&now, a);
+}