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

Wallclocktime strftime #483

Merged
merged 3 commits into from
Feb 4, 2025
Merged
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
10 changes: 3 additions & 7 deletions lib/filterx/object-datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,14 @@ _strftime_eval(FilterXExpr *s)
convert_unix_time_to_wall_clock_time (&datetime, &wct);

const gsize MAX_RESULT_STR_LEN = 256;
size_t date_size = 0;
gchar result_str[MAX_RESULT_STR_LEN];

// TODO - implement wall_clock_time_strftime, because there is some inconsistency with the format accepted by wall_clock_time_strptime
date_size = strftime(result_str, MAX_RESULT_STR_LEN, self->format, &wct.tm);
size_t date_size = wall_clock_time_strftime(&wct, result_str, MAX_RESULT_STR_LEN, self->format);

if (!date_size)
{
return filterx_null_new();
}
return filterx_null_new();

return filterx_string_new(result_str, strnlen(result_str, MAX_RESULT_STR_LEN));
return filterx_string_new(result_str, date_size);
}

static FilterXExpr *
Expand Down
65 changes: 64 additions & 1 deletion lib/timeutils/tests/test_wallclocktime.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ Test(wallclocktime, test_strptime_zone_parsing_takes_daylight_saving_into_accoun
cr_expect(wct.wct_gmtoff == 1*3600, "Unexpected timezone offset: %ld, expected 1*3600", wct.wct_gmtoff);

}

static void
_guess_missing_year(WallClockTime *wct, gint mon)
{
Expand Down Expand Up @@ -496,6 +495,70 @@ Test(wallclocktime, test_strptime_percent_z_is_mandatory)
cr_assert_null(wall_clock_time_strptime(&wct, "%Y-%m-%d %T%z", "2011-06-25 20:00:04"));
}

Test(wallclocktime, test_strftime_percent_f)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar buf[128];

wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f %z", "May 7 2021 09:29:12.123456 CEST");

wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%f");
cr_assert_str_eq(buf, ".123456");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%3f");
cr_assert_str_eq(buf, ".123");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%6f");
cr_assert_str_eq(buf, ".123456");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%9f");
cr_assert_str_eq(buf, ".123456");

wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f %z", "May 7 2021 09:29:12.012345 CEST");

wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%f");
cr_assert_str_eq(buf, ".012345");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%3f");
cr_assert_str_eq(buf, ".012");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%6f");
cr_assert_str_eq(buf, ".012345");
wall_clock_time_strftime(&wct, buf, sizeof(buf), ".%9f");
cr_assert_str_eq(buf, ".012345");
}

Test(wallclocktime, test_strftime_can_be_parsed_by_strptime)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
WallClockTime wct2 = WALL_CLOCK_TIME_INIT;
gchar buf[128];

wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f %z", "May 7 2021 09:29:12.123456+02:00");
wall_clock_time_strftime(&wct, buf, sizeof(buf), "%b %d %Y %H:%M:%S.%f %z");
wall_clock_time_strptime(&wct2, "%b %d %Y %H:%M:%S.%f %z", buf);

cr_expect(wct.wct_year == wct2.wct_year);
cr_expect(wct.wct_mon == wct2.wct_mon);
cr_expect(wct.wct_mday == wct2.wct_mday);

cr_expect(wct.wct_hour == wct2.wct_hour);
cr_expect(wct.wct_min == wct2.wct_min);
cr_expect(wct.wct_sec == wct2.wct_sec);
cr_expect(wct.wct_usec == wct2.wct_usec);

cr_expect(wct.wct_isdst == wct2.wct_isdst, "%d != %d", wct.wct_isdst, wct2.wct_isdst);
cr_expect(wct.wct_gmtoff == wct2.wct_gmtoff);
}

Test(wallclocktime, test_strftime_all_format_spec)
{
WallClockTime wct = WALL_CLOCK_TIME_INIT;
gchar buf[256];

wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S.%f %z", "Aug 7 2021 09:29:12.123456+02:00");
wall_clock_time_strftime(&wct, buf, sizeof(buf),
"%a %A %b %B '%c' %C %d '%D' '%e' %f '%F' %g %G %h %H %I %j %m %M %n %p %r %R %s %S %t '%T' %u %U %W %V %w %x %X %y %Y %z %Z");
cr_assert_str_eq(buf,
"Fri Friday Aug August 'Fri Aug 7 09:29:12 2021' 20 07 '08/07/21' ' 7' 123456 '2021-08-07' 21 2021 Aug 09 09 219 08 29 \n"
" AM 09:29:12 AM 09:29 1628324952 12 \t '10:29:12' 6 31 31 31 6 08/07/21 10:29:12 21 2021 +0200 +02:00");
}

static void
setup(void)
{
Expand Down
Loading