Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add %F output format option to print timestamps in a human-readable format #351

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <time.h>

#include "kcat.h"
#include "rdendian.h"

Expand Down Expand Up @@ -136,6 +139,10 @@ void fmt_parse (const char *fmt) {
fmt_add(KC_FMT_TIMESTAMP, NULL, 0);
conf.flags |= CONF_F_APIVERREQ;
break;
case 'D':
fmt_add(KC_FMT_FORMATTED_TIMESTAMP, NULL, 0);
conf.flags |= CONF_F_APIVERREQ;
break;
#if HAVE_HEADERS
case 'h':
fmt_add(KC_FMT_HEADERS, NULL, 0);
Expand Down Expand Up @@ -370,6 +377,36 @@ static int unpack (FILE *fp, const char *what, const char *fmt,
}


/**
* Convert timestamp into a human-readable format.
*/
void format_timestamp(char *dst, size_t maxsize, int64_t unixtime_ms) {
time_t unixtime_secs = unixtime_ms / 1000;

struct tm *ts = localtime(&unixtime_secs);

size_t pos = strftime(dst, maxsize,
"%Y-%m-%dT%H:%M:%S.", ts);

if (pos == 0) {
return;
}

// add milliseconds
pos += snprintf(
&dst[pos],
maxsize-pos,
"%" PRId64,
unixtime_ms % 1000);

alexey-sveshnikov marked this conversation as resolved.
Show resolved Hide resolved
if (pos >= maxsize) {
return;
}

// add timezone
strftime(&dst[pos], maxsize-pos, "%z", ts);
}



/**
Expand Down Expand Up @@ -517,6 +554,24 @@ static void fmt_msg_output_str (FILE *fp,
&tstype));
#else
r = fprintf(fp, "-1");
#endif
break;
}

case KC_FMT_FORMATTED_TIMESTAMP:
{
#if RD_KAFKA_VERSION >= 0x000902ff
rd_kafka_timestamp_type_t tstype;

char buf[80];
format_timestamp(buf,
sizeof(buf),
rd_kafka_message_timestamp(rkmessage,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check that tstype is valid, on older brokers (or message format versions) the tstype will be NONE and the timestamp unusable.

&tstype));

r = fprintf(fp, "%s", buf);
#else
r = fprintf(fp, "-1");
#endif
break;
}
Expand Down
1 change: 1 addition & 0 deletions kcat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,7 @@ static void RD_NORETURN usage (const char *argv0, int exitcode,
" %%K Message key length (or -1 for NULL)\n"
#if RD_KAFKA_VERSION >= 0x000902ff
" %%T Message timestamp (milliseconds since epoch UTC)\n"
" %%D Message timestamp (ISO8601 formatted)\n"
#endif
#if HAVE_HEADERS
" %%h Message headers (n=v CSV)\n"
Expand Down
1 change: 1 addition & 0 deletions kcat.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ typedef enum {
KC_FMT_TOPIC,
KC_FMT_PARTITION,
KC_FMT_TIMESTAMP,
KC_FMT_FORMATTED_TIMESTAMP,
KC_FMT_HEADERS
} fmt_type_t;

Expand Down