Skip to content

Commit ccd8c53

Browse files
authored
pack: add java_sql_timestamp format (#4822)
This commit adds a new timestamp format, the java_sql_timestamp, which is very similar to iso8601, except that it has a space instead of a T between the date and the hour and does not end with Z (or any other timestamp delimiter) This is the format: "%Y-%m-%d %H:%M:%S" This is unfortunatelly the only format accepted by Amazon Athena. Signed-off-by: Marcos Diez <[email protected]>
1 parent b145d1b commit ccd8c53

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

include/fluent-bit/flb_pack.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@
3636
#define FLB_PACK_JSON_PRIMITIVE JSMN_PRIMITIVE
3737

3838
/* Date formats */
39-
#define FLB_PACK_JSON_DATE_DOUBLE 0
40-
#define FLB_PACK_JSON_DATE_ISO8601 1
41-
#define FLB_PACK_JSON_DATE_EPOCH 2
39+
#define FLB_PACK_JSON_DATE_DOUBLE 0
40+
#define FLB_PACK_JSON_DATE_ISO8601 1
41+
#define FLB_PACK_JSON_DATE_EPOCH 2
42+
#define FLB_PACK_JSON_DATE_JAVA_SQL_TIMESTAMP 3
4243

4344
/* Specific ISO8601 format */
4445
#define FLB_PACK_JSON_DATE_ISO8601_FMT "%Y-%m-%dT%H:%M:%S"
4546

47+
/* Specific Java SQL Timestamp format */
48+
#define FLB_PACK_JSON_DATE_JAVA_SQL_TIMESTAMP_FMT "%Y-%m-%d %H:%M:%S"
49+
4650
/* JSON formats (modes) */
4751
#define FLB_PACK_JSON_FORMAT_NONE 0
4852
#define FLB_PACK_JSON_FORMAT_JSON 1

src/flb_pack.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ int flb_pack_to_json_date_type(const char *str)
774774
if (strcasecmp(str, "double") == 0) {
775775
return FLB_PACK_JSON_DATE_DOUBLE;
776776
}
777+
else if (strcasecmp(str, "java_sql_timestamp") == 0) {
778+
return FLB_PACK_JSON_DATE_JAVA_SQL_TIMESTAMP;
779+
}
777780
else if (strcasecmp(str, "iso8601") == 0) {
778781
return FLB_PACK_JSON_DATE_ISO8601;
779782
}
@@ -883,6 +886,20 @@ flb_sds_t flb_pack_msgpack_to_json_format(const char *data, uint64_t bytes,
883886
case FLB_PACK_JSON_DATE_DOUBLE:
884887
msgpack_pack_double(&tmp_pck, flb_time_to_double(&tms));
885888
break;
889+
case FLB_PACK_JSON_DATE_JAVA_SQL_TIMESTAMP:
890+
/* Format the time, use microsecond precision not nanoseconds */
891+
gmtime_r(&tms.tm.tv_sec, &tm);
892+
s = strftime(time_formatted, sizeof(time_formatted) - 1,
893+
FLB_PACK_JSON_DATE_JAVA_SQL_TIMESTAMP_FMT, &tm);
894+
895+
len = snprintf(time_formatted + s,
896+
sizeof(time_formatted) - 1 - s,
897+
".%06" PRIu64,
898+
(uint64_t) tms.tm.tv_nsec / 1000);
899+
s += len;
900+
msgpack_pack_str(&tmp_pck, s);
901+
msgpack_pack_str_body(&tmp_pck, time_formatted, s);
902+
break;
886903
case FLB_PACK_JSON_DATE_ISO8601:
887904
/* Format the time, use microsecond precision not nanoseconds */
888905
gmtime_r(&tms.tm.tv_sec, &tm);

0 commit comments

Comments
 (0)