+GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
+ AvahiUsec u;
+ g_assert(a);
+
+ u = usec + a->tv_usec;
+
+ if (u < 0) {
+ a->tv_usec = (glong) (1000000 + (u % 1000000));
+ a->tv_sec += (glong) (-1 + (u / 1000000));
+ } else {
+ a->tv_usec = (glong) (u % 1000000);
+ a->tv_sec += (glong) (u / 1000000);
+ }
+
+ return a;
+}
+
+AvahiUsec avahi_age(const GTimeVal *a) {
+ GTimeVal now;
+
+ g_assert(a);
+
+ g_get_current_time(&now);
+
+ return avahi_timeval_diff(&now, a);
+}
+
+GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
+ g_assert(tv);
+
+ g_get_current_time(tv);
+
+ if (msec)
+ avahi_timeval_add(tv, (AvahiUsec) msec*1000);
+
+ if (jitter)
+ avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
+
+ return tv;
+}