Extend strings API for formatted dates

Add two new functions string_catftime(), resp. string_strftime(), which
can be used to append, resp. print, a formatted date to a dynamic
string.

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2017-08-21 21:07:50 +02:00
parent 0c63b4661f
commit 550a2e9379
2 changed files with 22 additions and 0 deletions

View File

@ -1028,6 +1028,8 @@ char *string_buf(struct string *);
int string_catf(struct string *, const char *, ...);
int string_vcatf(struct string *, const char *, va_list);
int string_printf(struct string *, const char *, ...);
int string_catftime(struct string *, const char *, const struct tm *);
int string_strftime(struct string *, const char *, const struct tm *);
/* todo.c */
extern llist_t todolist;

View File

@ -112,3 +112,23 @@ int string_printf(struct string *sb, const char *format, ...)
return n;
}
int string_catftime(struct string *sb, const char *format, const struct tm *tm)
{
int n = 0;
while (!n) {
string_grow(sb, sb->bufsize * 2);
n = strftime(sb->buf + sb->len, sb->bufsize - sb->len, format,
tm);
}
sb->len += n;
return n;
}
int string_strftime(struct string *sb, const char *format, const struct tm *tm)
{
string_reset(sb);
return string_catftime(sb, format, tm);
}