-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint_progress.c
35 lines (28 loc) · 1.1 KB
/
print_progress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <time.h>
#include <string.h>
#include <stdio.h>
void print_progress( FILE* file, time_t start_time, long long unsigned final, long long unsigned so_far )
{
time_t now = time(NULL);
if ( now > start_time ) {
long long unsigned left = final - so_far;
double rate = ( so_far / (double)difftime(now, start_time));
time_t done_at = (time_t)(now + ( left / rate ));
double percent_done = ( so_far / (double) final ) * 100;
struct tm *the_time = localtime( &done_at);
char time_buf[32];
char now_buf[32];
char format_buf[128] = " %s : [%10llu / %10llu] %10.2lf per second, %6.2lf%% done, finishing at %s";
int eol_char_idx = strlen( format_buf );
if ( so_far >= final ) {
format_buf[eol_char_idx] = '\n';
} else {
format_buf[eol_char_idx] = '\r';
}
format_buf[ eol_char_idx + 1 ] = '\0';
strftime( time_buf, 256, "%Y-%m-%d %H:%M:%S", the_time );
strftime( now_buf, 256, "%Y-%m-%d %H:%M:%S", localtime( &now ));
fprintf(file , format_buf, now_buf, so_far, final, rate, percent_done, time_buf );
fflush(file);
}
}