+/* Calculate time remaining until *exp, return 0 if now >= *exp */
+static int time_left(struct timeval *diff, struct timeval *exp)
+{
+ struct timeval now;
+
+ if (gettimeofday(&now, NULL) < 0) {
+ error("gettimeofday: %m");
+ return 0;
+ }
+
+ if (now.tv_sec > exp->tv_sec
+ || (now.tv_sec == exp->tv_sec && now.tv_usec >= exp->tv_usec))
+ return 0;
+
+ diff->tv_sec = exp->tv_sec - now.tv_sec;
+ diff->tv_usec = exp->tv_usec - now.tv_usec;
+ if (diff->tv_usec < 0) {
+ diff->tv_usec += 1000000;
+ --diff->tv_sec;
+ }
+
+ return 1;
+}
+