Skip to content

Commit

Permalink
Add ctime and iso to print_timestamp_format()
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnw-sebast committed Jun 1, 2024
1 parent 047ee47 commit aa24dbf
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ int main(int argc, char **argv)
{ "vcount", 'C', OPTPARSE_REQUIRED },
{ "rdns", 'd', OPTPARSE_NONE },
{ "timestamp", 'D', OPTPARSE_NONE },
{ "timestamp-format", '0', OPTPARSE_NONE },
{ "timestamp-format", '0', OPTPARSE_REQUIRED },
{ "elapsed", 'e', OPTPARSE_NONE },
{ "file", 'f', OPTPARSE_REQUIRED },
{ "generate", 'g', OPTPARSE_NONE },
Expand Down Expand Up @@ -566,7 +566,13 @@ int main(int argc, char **argv)
switch (c) {
case '0':
if(strstr(optparse_state.optlongname, "timestamp-format") != NULL) {
timestamp_format_flag = 1;
if(strcmp(optparse_state.optarg, "ctime") == 0) {
timestamp_format_flag = 1;
}else if(strcmp(optparse_state.optarg, "iso") == 0) {
timestamp_format_flag = 2;
}else{
usage(1);
}
}
break;
case '4':
Expand Down Expand Up @@ -2958,11 +2964,15 @@ void print_timestamp_format(int64_t current_time_ns, int timestamp_format)
time_t current_time_s;
struct tm *local_time;

if(timestamp_format == 1)
{
if(timestamp_format == 1 || timestamp_format == 2) {
current_time_s = current_time_ns / 1000000000;
local_time = localtime(&current_time_s);
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", local_time);
if(timestamp_format == 1) {
strftime(time_buffer, sizeof(time_buffer), "%c", local_time);
}
else if(timestamp_format == 2) {
strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%dT%H:%M:%S", local_time);
}
printf("[%s] ", time_buffer);
}else{
printf("[%.5f] ", (double)current_time_ns / 1e9);
Expand Down Expand Up @@ -3023,7 +3033,7 @@ void usage(int is_error)
fprintf(out, " -C, --vcount=N same as -c, report results (not stats) in verbose format\n");
fprintf(out, " -d, --rdns show targets by name (force reverse-DNS lookup)\n");
fprintf(out, " -D, --timestamp print timestamp before each output line\n");
fprintf(out, " --timestamp-format required -D and print timestamp in human readable format\n");
fprintf(out, " --timestamp-format=FORMAT show timestamp using the given format (-D are required): [ctime|iso]\n");
fprintf(out, " -e, --elapsed show elapsed time on return packets\n");
fprintf(out, " -n, --name show targets by name (reverse-DNS lookup for target IPs)\n");
fprintf(out, " -N, --netdata output compatible for netdata (-l -Q are required)\n");
Expand Down

0 comments on commit aa24dbf

Please sign in to comment.