From 550a2e937906d7e3c821d7508901eea2dafd9344 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Mon, 21 Aug 2017 21:07:50 +0200 Subject: 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 --- src/strings.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/strings.c') diff --git a/src/strings.c b/src/strings.c index 816ddc1..6601a99 100644 --- a/src/strings.c +++ b/src/strings.c @@ -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); +} -- cgit v1.2.3-54-g00ecf