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

Print human readable timestamp string #216

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
25 changes: 22 additions & 3 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include "kafkacat.h"
#include "rdendian.h"
#include <time.h>

static void fmt_add (fmt_type_t type, const char *str, int len) {
if (conf.fmt_cnt == KC_FMT_MAX_SIZE)
Expand Down Expand Up @@ -370,6 +371,21 @@ static int unpack (FILE *fp, const char *what, const char *fmt,
}


void timestamp_as_utc_string(char *dst, int64_t epoch) {
time_t seconds = (time_t)(epoch/1000);
int milliseconds = epoch % 1000;
struct tm ts;

// Format time, "ddd yyyy-mm-dd hh:mm:ss.xxx zzz", xxx is place for milliseconds
ts = *localtime(&seconds);
strftime(dst, 64, "%a %Y-%m-%d %H:%M:%S.xxx %Z", &ts);

char* p;
p = strstr(dst, "xxx");
char ms[4];
sprintf(ms, "%03d", milliseconds);
strncpy(p, ms, 3);
}


/**
Expand Down Expand Up @@ -511,9 +527,12 @@ static void fmt_msg_output_str (FILE *fp,
{
#if RD_KAFKA_VERSION >= 0x000902ff
rd_kafka_timestamp_type_t tstype;
r = fprintf(fp, "%"PRId64,
rd_kafka_message_timestamp(rkmessage,
&tstype));
int64_t epoch = rd_kafka_message_timestamp(rkmessage, &tstype);

char buf[80];
timestamp_as_utc_string(buf, epoch);

r = fprintf(fp, "%"PRId64" (%s)", epoch, buf);
#else
r = fprintf(fp, "-1");
#endif
Expand Down