Skip to content

Instantly share code, notes, and snippets.

@bitsmanent
Created August 27, 2022 20:31
Show Gist options
  • Save bitsmanent/2e2b3c78f2d5ff0c15915268ddc3ea1f to your computer and use it in GitHub Desktop.
Save bitsmanent/2e2b3c78f2d5ff0c15915268ddc3ea1f to your computer and use it in GitHub Desktop.
strftime(3) with proper return values
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