diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-09-22 17:32:44 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2026-02-13 09:57:39 +0100 |
| commit | 78bf2fe4a5bf37514f6dfd203ef969da0bf40c2e (patch) | |
| tree | 33f9440b8ee0fa7a3b3ad033616d722d2101bb4d /astrftime.c | |
| parent | 107a2e43d54f9a42fb85b00b83cb0d9abb194680 (diff) | |
Diffstat (limited to 'astrftime.c')
| -rw-r--r-- | astrftime.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/astrftime.c b/astrftime.c new file mode 100644 index 0000000..4c10654 --- /dev/null +++ b/astrftime.c @@ -0,0 +1,58 @@ +#define _POSIX_C_SOURCE 200809L + +#include "utils.h" +#include <errno.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +char *astrftime(const char *const fmt, const struct tm *const tm) +{ + char *ret = NULL, *s = NULL; + size_t n = sizeof "."; + const locale_t l = newlocale(LC_TIME_MASK, "POSIX", (locale_t)0); + + if (l == (locale_t)0) + { + fprintf(stderr, "%s: newlocale(3): %s\n", __func__, strerror(errno)); + goto end; + } + else if (!(s = malloc(n))) + { + fprintf(stderr, "%s: malloc(3): %s\n", __func__, strerror(errno)); + goto end; + } + + for (;;) + { + const int res = strftime_l(s, n, fmt, tm, l); + + if (!res) + { + char *const ss = realloc(s, ++n); + + if (!s) + { + fprintf(stderr, "%s: realloc(3): %s\n", __func__, + strerror(errno)); + goto end; + } + + s = ss; + } + else + break; + } + + ret = s; + +end: + + if (!ret) + free(s); + + freelocale(l); + return ret; +} |
