about summary refs log tree commit diff
path: root/timeutil.c
diff options
context:
space:
mode:
authorsternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-20 17:32:20 +0200
committersternenseemann <0rpkxez4ksa01gb3typccl0i@systemli.org>2020-08-20 17:32:20 +0200
commitcada2ed89e5a0b68e2cfd002bf707107fb6d7111 (patch)
tree6872f2fa3907dfd0e33277f99ba1a49a88e10a19 /timeutil.c
parentf5f1eb61204bea7ea21af9b9964b63ffb00f2184 (diff)
feat(rss): minor improvements and refactor time formatting
* add timeutil.{c,h}: utility for formatting timestamps
* wrap feed description in CDATA
* use xml_escaped where applicable
* use time of latest entry for lastBuildDate
Diffstat (limited to 'timeutil.c')
-rw-r--r--timeutil.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/timeutil.c b/timeutil.c
new file mode 100644
index 0000000..3d1cec3
--- /dev/null
+++ b/timeutil.c
@@ -0,0 +1,30 @@
+#define _POSIX_C_SOURCE 1
+#include <time.h>
+#include "timeutil.h"
+
+extern long timezone;
+
+char *format_string(enum time_format t, long tz) {
+    switch(t) {
+        case RSS_TIME_FORMAT:
+            return "%a, %d %b %Y %T %z";
+        case HTML_TIME_FORMAT_READABLE:
+            return tz == 0 ? "%Y-%m-%d %TZ" : "%Y-%m-%d %T%z";
+        case ATOM_TIME_FORMAT:
+        default:
+            return tz == 0 ? "%Y-%m-%dT%TZ" : "%Y-%m-%dT%T%z";
+    }
+}
+
+size_t flocaltime(char *b, enum time_format type, size_t size, const time_t *time) {
+    tzset();
+    struct tm *local = localtime(time);
+    char *format = format_string(type, timezone);
+
+    size_t res = strftime(b, size, format, local);
+
+    // prevent any buffer overflows
+    b[size - 1] = '\0';
+
+    return res;
+}