commit 1a8980feddb0b569d0f1747c0a831efc00348ddb
parent b07356835d48aed99ad9097d85af67e292a7f3d9
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Tue, 19 Mar 2024 19:16:08 -0700
Add html <time> helper functions
Diffstat:
2 files changed, 35 insertions(+), 0 deletions(-)
diff --git a/html.c b/html.c
@@ -1,6 +1,7 @@
#include <sys/types.h>
#include <stdarg.h>
#include <stdint.h>
+#include <time.h>
#include <kcgi.h>
#include <kcgihtml.h>
@@ -42,3 +43,35 @@ htmlscript(struct pagedata *pd, char *src)
return status;
return KCGI_OK;
}
+
+enum kcgi_err
+htmldatetime(struct pagedata *pd, char *datetime, char *text)
+{
+ enum kcgi_err status;
+
+ if (!text)
+ text = datetime;
+ else if (!datetime)
+ datetime = text;
+
+ assert(datetime && text);
+
+ if ((status = khtml_attr(&pd->html, KELEM_TIME,
+ KATTR_DATETIME, datetime, KATTR__MAX)) != KCGI_OK ||
+ (status = khtml_puts(&pd->html, text)) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 1)))
+ return status;
+ return KCGI_OK;
+}
+
+enum kcgi_err
+htmldate(struct pagedata *pd, time_t time)
+{
+ char buf[11];
+ struct tm *tm;
+
+ tm = gmtime(&time);
+ if (!tm || strftime(buf, sizeof(buf), "%F", tm) >= sizeof(buf))
+ return KCGI_SYSTEM;
+ return htmldatetime(pd, buf, NULL);
+}
diff --git a/html.h b/html.h
@@ -1,3 +1,5 @@
enum kcgi_err htmlwithin(struct pagedata *pd, enum kelem e, char *text);
enum kcgi_err htmllink(struct pagedata *pd, char *link, char *text);
enum kcgi_err htmlscript(struct pagedata *pd, char *src);
+enum kcgi_err htmldatetime(struct pagedata *pd, char *datetime, char *text);
+enum kcgi_err htmldate(struct pagedata *pd, time_t time);