From 65bd0ac38ca8e68940008689114a53bc195c8f7f Mon Sep 17 00:00:00 2001 From: Balazs Scheidler Date: Wed, 29 Jan 2025 11:26:00 +0100 Subject: [PATCH] wallclocktime: optionally skip the leading dot in strptime(%f) parsing Signed-off-by: Balazs Scheidler --- lib/timeutils/tests/test_wallclocktime.c | 19 +++++++++++++++++++ lib/timeutils/wallclocktime.c | 2 ++ 2 files changed, 21 insertions(+) diff --git a/lib/timeutils/tests/test_wallclocktime.c b/lib/timeutils/tests/test_wallclocktime.c index e511331e5a..73f5d02ad5 100644 --- a/lib/timeutils/tests/test_wallclocktime.c +++ b/lib/timeutils/tests/test_wallclocktime.c @@ -75,6 +75,25 @@ Test(wallclocktime, test_strptime_parses_broken_down_time) cr_expect(wct.wct_gmtoff == -1); } +Test(wallclocktime, test_strptime_percentf_skips_optional_dot) +{ + WallClockTime wct = WALL_CLOCK_TIME_INIT; + gchar *end; + + end = wall_clock_time_strptime(&wct, "%b %d %Y %H:%M:%S%f", "Jan 16 2019 18:23:12.012345"); + cr_assert(*end == 0); + cr_expect(wct.wct_year == 119); + cr_expect(wct.wct_mon == 0); + cr_expect(wct.wct_mday == 16); + + cr_expect(wct.wct_hour == 18); + cr_expect(wct.wct_min == 23); + cr_expect(wct.wct_sec == 12); + cr_expect(wct.wct_usec == 12345); + + cr_expect(wct.wct_gmtoff == -1); +} + Test(wallclocktime, test_strptime_parses_truncated_usec) { WallClockTime wct = WALL_CLOCK_TIME_INIT; diff --git a/lib/timeutils/wallclocktime.c b/lib/timeutils/wallclocktime.c index 64b321c782..47bee22620 100644 --- a/lib/timeutils/wallclocktime.c +++ b/lib/timeutils/wallclocktime.c @@ -421,6 +421,8 @@ wall_clock_time_strptime(WallClockTime *wct, const gchar *format, const gchar *i case 'f': { + if (*bp == '.') + bp++; const unsigned char *end = conv_num(bp, &wct->wct_usec, 0, 999999); if (!end) return NULL;