Add time_until() and time_since() utility functions

Adding these in preparation for converting server->connect_time from time_t
to struct timeval.  Also converts three existing open-coded versions over to
the new functions.
This commit is contained in:
Kevin Easton
2015-05-13 21:50:30 +10:00
parent 0f8280ed51
commit 4ee6c6295b
4 changed files with 23 additions and 3 deletions

View File

@@ -84,6 +84,8 @@ int lw_strcmp (comp_func *, char *, char *);
int open_to (char *, int, off_t); int open_to (char *, int, off_t);
struct timeval BX_get_time (struct timeval *); struct timeval BX_get_time (struct timeval *);
double BX_time_diff (struct timeval, struct timeval); double BX_time_diff (struct timeval, struct timeval);
double time_since(const struct timeval *tv_from);
double time_until(const struct timeval *tv_to);
char * BX_plural (int); char * BX_plural (int);
int BX_time_to_next_minute (void); int BX_time_to_next_minute (void);
char * BX_remove_trailing_spaces (char *); char * BX_remove_trailing_spaces (char *);

View File

@@ -5531,7 +5531,7 @@ struct timeval start;
* with the arguments. * with the arguments.
*/ */
add_timer(0, empty_string, milliseconds, 1, (int (*)(void *, char *))comment, NULL, NULL, get_current_winref(), "pause"); add_timer(0, empty_string, milliseconds, 1, (int (*)(void *, char *))comment, NULL, NULL, get_current_winref(), "pause");
while (BX_time_diff(get_time(NULL), start) > 0) while (time_until(&start) > 0)
io("e_pause"); io("e_pause");
} }

View File

@@ -1617,7 +1617,7 @@ char lame_type[30];
strcpy(lame_type, "T"); strcpy(lame_type, "T");
strcat(lame_type, dcc_types[type]->name); strcat(lame_type, dcc_types[type]->name);
xtime = BX_time_diff(n->starttime, get_time(NULL)); xtime = time_since(&n->starttime);
xfer = (double)(n->bytes_sent ? n->bytes_sent : n->bytes_read); xfer = (double)(n->bytes_sent ? n->bytes_sent : n->bytes_read);
if (xfer == 0.0) if (xfer == 0.0)
@@ -2884,7 +2884,7 @@ DCC_int *n;
return; return;
dcc_bytes_in += n->bytes_read; dcc_bytes_in += n->bytes_read;
dcc_bytes_out += n->bytes_sent; dcc_bytes_out += n->bytes_sent;
xtime = BX_time_diff(n->starttime, get_time(NULL)); xtime = time_since(&n->starttime);
if (xtime <= 0) if (xtime <= 0)
xtime = 1; xtime = 1;

View File

@@ -1746,6 +1746,24 @@ double BX_time_diff (struct timeval one, struct timeval two)
return (double)td.tv_sec + ((double)td.tv_usec / 1000000.0); return (double)td.tv_sec + ((double)td.tv_usec / 1000000.0);
} }
/* Calculate time elapsed since a time in the past. */
double time_since(const struct timeval *tv_from)
{
struct timeval tv_now;
get_time(&tv_now);
return BX_time_diff(*tv_from, tv_now);
}
/* Calculate time from now until a time in the future. */
double time_until(const struct timeval *tv_to)
{
struct timeval tv_now;
get_time(&tv_now);
return BX_time_diff(tv_now, *tv_to);
}
int BX_time_to_next_minute (void) int BX_time_to_next_minute (void)
{ {
time_t now = time(NULL); time_t now = time(NULL);