Fix time-related unit tests on alpine linux#4069
Open
clumens wants to merge 4 commits intoClusterLabs:mainfrom
Open
Fix time-related unit tests on alpine linux#4069clumens wants to merge 4 commits intoClusterLabs:mainfrom
clumens wants to merge 4 commits intoClusterLabs:mainfrom
Conversation
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.
nrwahl2
reviewed
Mar 12, 2026
| */ | ||
| assert_hr_format("%3S seconds", "0" SECOND_S " seconds", | ||
| // *BSD strftime() doesn't support field widths | ||
| #ifdef HAVE_STRFTIME_WIDTH |
Contributor
There was a problem hiding this comment.
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>]], [[ |
Contributor
There was a problem hiding this comment.
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); |
Contributor
There was a problem hiding this comment.
Are we missing a space here?
SECOND_S " seconds"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@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.