Created
August 27, 2022 20:31
-
-
Save bitsmanent/2e2b3c78f2d5ff0c15915268ddc3ea1f to your computer and use it in GitHub Desktop.
strftime(3) with proper return values
This file contains 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
int | |
xstrftime(char *s, size_t max, const char *format, const struct tm *tm) { | |
char *fmt; | |
int slen, flen; | |
flen = strlen(format); | |
if(!(fmt = malloc(flen + 1))) | |
return -1; | |
fmt[0] = 'x'; | |
memcpy(&fmt[1], format, flen); | |
slen = strftime(s, max, fmt, tm) - 1; | |
if(slen > 0) | |
memmove(s, &s[1], max); | |
free(fmt); | |
return slen; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment