summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2017-08-21 21:07:50 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2017-08-28 07:30:38 +0200
commit550a2e937906d7e3c821d7508901eea2dafd9344 (patch)
treec94df0db850d9d9e2455bf74d5b2d6a228db5948
parent0c63b4661f5006348f384519eb6a9eecfa3c3fb7 (diff)
downloadcalcurse-550a2e937906d7e3c821d7508901eea2dafd9344.tar.gz
calcurse-550a2e937906d7e3c821d7508901eea2dafd9344.zip
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>
-rw-r--r--src/calcurse.h2
-rw-r--r--src/strings.c20
2 files changed, 22 insertions, 0 deletions
diff --git a/src/calcurse.h b/src/calcurse.h
index 8bcb48a..4ddb066 100644
--- a/src/calcurse.h
+++ b/src/calcurse.h
@@ -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;
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);
+}