Skip to content

Commit

Permalink
aws_util: fix leading zeros in time_key strings
Browse files Browse the repository at this point in the history
Signed-off-by: kangaechu <kangae2@gmail.com>
  • Loading branch information
kangaechu committed Jul 25, 2024
1 parent ac4a8aa commit ef78b47
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/aws/flb_aws_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1003,9 +1003,9 @@ size_t flb_aws_strftime_precision(char **out_buf, const char *time_format,

/* Replace %3N to millisecond, %9N and %L to nanosecond in time_format. */
snprintf(millisecond_str, FLB_AWS_MILLISECOND_FORMATTER_LENGTH+1,
"%" PRIu64, (uint64_t) tms->tm.tv_nsec / 1000000);
"%03" PRIu64, (uint64_t) tms->tm.tv_nsec / 1000000);
snprintf(nanosecond_str, FLB_AWS_NANOSECOND_FORMATTER_LENGTH+1,
"%" PRIu64, (uint64_t) tms->tm.tv_nsec);
"%09" PRIu64, (uint64_t) tms->tm.tv_nsec);
for (i = 0; i < time_format_len; i++) {
if (strncmp(time_format+i, FLB_AWS_MILLISECOND_FORMATTER, 3) == 0) {
strncat(tmp_parsed_time_str, millisecond_str,
Expand Down
16 changes: 16 additions & 0 deletions tests/internal/aws_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,21 @@ static void test_flb_get_s3_key_mixed_timestamp()
flb_sds_destroy(s3_key_format);
}

static void test_flb_aws_strftime_precision_leading_zeros()
{
char *out_buf;
struct flb_time tms;
char *time_format = "ms=%3N, ns=%9N, ns=%L";
tms.tm.tv_sec = 1698784683;
tms.tm.tv_nsec = 1000009;

out_buf = flb_malloc(1024);
TEST_CHECK(out_buf != NULL);
flb_aws_strftime_precision(&out_buf, time_format, &tms);

TEST_CHECK(strcmp(out_buf, "ms=001, ns=001000009, ns=001000009") == 0);
}

TEST_LIST = {
{ "parse_api_error" , test_flb_aws_error},
{ "flb_aws_endpoint" , test_flb_aws_endpoint},
Expand All @@ -391,5 +406,6 @@ TEST_LIST = {
{"flb_get_s3_key_increment_index", test_flb_get_s3_key_increment_index},
{"flb_get_s3_key_index_overflow", test_flb_get_s3_key_index_overflow},
{"flb_get_s3_key_mixed_timestamp", test_flb_get_s3_key_mixed_timestamp},
{"test_flb_aws_strftime_precision_leading_zeros", test_flb_aws_strftime_precision_leading_zeros},
{ 0 }
};

0 comments on commit ef78b47

Please sign in to comment.