diff --git a/src/fping.c b/src/fping.c index 6d9e77c..d964b75 100644 --- a/src/fping.c +++ b/src/fping.c @@ -401,7 +401,7 @@ void host_add_timeout_event(HOST_ENTRY *h, int index, int64_t ev_time); struct event *host_get_timeout_event(HOST_ENTRY *h, int index); void stats_add(HOST_ENTRY *h, int index, int success, int64_t latency); void update_current_time(); -void print_human_readable_time(int64_t current_time_ns); +void print_timestamp_format(int64_t current_time_ns, int timestamp_format); /************************************************************ @@ -1382,10 +1382,8 @@ void main_loop() stats_add(h, event->ping_index, 0, -1); if (per_recv_flag) { - if (timestamp_flag && timestamp_format_flag == 0) { - printf("[%.5f] ", (double)current_time_ns / 1e9); - } else if(timestamp_flag && timestamp_format_flag == 1) { - print_human_readable_time(current_time_ns); + if (timestamp_flag) { + print_timestamp_format(current_time_ns, timestamp_format_flag); } printf("%-*s : [%d], timed out", max_hostname_len, h->host, event->ping_index); @@ -2497,10 +2495,8 @@ int wait_for_reply(int64_t wait_time) /* print received ping (unless --quiet) */ if (per_recv_flag) { - if (timestamp_flag && timestamp_format_flag == 0) { - printf("[%.5f] ", (double)recv_time / 1e9); - } else if (timestamp_flag && timestamp_format_flag == 1) { - print_human_readable_time(recv_time); + if (timestamp_flag) { + print_timestamp_format(recv_time, timestamp_format_flag); } avg = h->total_time / h->num_recv; printf("%-*s : [%d], %d bytes, %s ms", @@ -2956,12 +2952,22 @@ void ev_remove(struct event_queue *queue, struct event *event) Function: print_human_readable_time from current_time_ns *************************************************************/ -void print_human_readable_time(int64_t current_time_ns) { - time_t current_time_s = current_time_ns / 1000000000; - struct tm *local_time = localtime(¤t_time_s); +void print_timestamp_format(int64_t current_time_ns, int timestamp_format) +{ char time_buffer[100]; - strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time); - printf("[%s] ", time_buffer); + time_t current_time_s; + struct tm *local_time; + + if(timestamp_format == 1) + { + current_time_s = current_time_ns / 1000000000; + local_time = localtime(¤t_time_s); + strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time); + printf("[%s] ", time_buffer); + }else{ + printf("[%.5f] ", (double)current_time_ns / 1e9); + } + } /************************************************************