Skip to content

Fix time-related unit tests on alpine linux#4069

Open
clumens wants to merge 4 commits intoClusterLabs:mainfrom
clumens:strftime
Open

Fix time-related unit tests on alpine linux#4069
clumens wants to merge 4 commits intoClusterLabs:mainfrom
clumens:strftime

Conversation

@clumens
Copy link
Contributor

@clumens clumens commented Mar 11, 2026

@nrwahl2 This came up in CI testing yesterday. @fabbione is probably going to want this in sooner rather than later, and I'm off the rest of this week. Feel free to modify this as appropriate and push. I have tested this on both Fedora and Alpine, but not FreeBSD (which is relevant). Luckily, most or all of this is going to go away when we can get rid of the strftime fallback, whenever that is.

clumens added 4 commits March 10, 2026 13:23
Not all platforms support this because it's a glibc extension.  For
instance, musl on Alpine Linux doesn't support it.
Alpine uses musl by default, which doesn't include a lot of the glibc
extensions including the field width specifier for strftime().
Unfortunately, it doesn't support it in a completely different way from
FreeBSD (and maybe other BSDs, but we don't support those).  So, we need
to handle each case differently in our tests.
On some platforms, like musl on Alpine Linux, the bare percent sign is
an error and the results will be an empty string.  Add a test for this.
Alpine uses musl by default, which handles bare percent signs in
strftime() differently.  On that platform, a bare percent sign (or an
unknown specifier) results in an empty string.  We need to check for
that and handle it in our test cases.
@clumens clumens requested a review from nrwahl2 March 11, 2026 13:15
*/
assert_hr_format("%3S seconds", "0" SECOND_S " seconds",
// *BSD strftime() doesn't support field widths
#ifdef HAVE_STRFTIME_WIDTH
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter but the following seems cleaner:

#if defined(HAVE_STRFTIME_WIDTH)
...
#elif defined(__FreeBSD__)
...
#else

[AC_DEFINE([HAVE_STRFTIME_WIDTH], [1],
[Define to 1 if strftime supports field width])])

AC_RUN_IFELSE([AC_LANG_PROGRAM([[#include <time.h>]], [[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good lord this is ugly. I looked around a bit for a cleaner way, but this seems like about the best we can do, other than potentially dropping bare declarations:

char s[200] = { '\0', };
time_t t = time(NULL);

return strftime(s, sizeof(s), "% abcd", localtime(&t)) != 0;

With that said, we use HAVE_STRFTIME_EMPTY_SPEC_FAILS only in one unit test file. So alternatively, we could add a setup function there that tests whether strftime() returns nonzero when there's an empty spec, and sets a file-scope variable if so.

I don't have strong feelings on it and it's not worth spending a ton of time on. Hopefully this all goes away in the future.

/* musl (and maybe other c libraries) ignore the field width
* and handle the rest of the specifier normally.
*/
SECOND_S "seconds", 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we missing a space here?

SECOND_S " seconds"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants