From adca120fdf42d8285d10173d584abec6a08ab1fc Mon Sep 17 00:00:00 2001 From: "Rafael G. Martins" Date: Sun, 3 Feb 2019 14:49:20 +0100 Subject: blogc: added datetime template variable --- src/blogc/funcvars.c | 4 ++++ src/blogc/sysinfo.c | 36 ++++++++++++++++++++++++++++++++++++ src/blogc/sysinfo.h | 6 ++++++ 3 files changed, 46 insertions(+) (limited to 'src') diff --git a/src/blogc/funcvars.c b/src/blogc/funcvars.c index 50abbb8..96bc3c5 100644 --- a/src/blogc/funcvars.c +++ b/src/blogc/funcvars.c @@ -30,6 +30,10 @@ static const struct func_map { {"BLOGC_SYSINFO_HOSTNAME", blogc_sysinfo_inject_hostname}, #endif +#ifdef HAVE_SYSINFO_DATETIME + {"BLOGC_SYSINFO_DATETIME", blogc_sysinfo_inject_datetime}, +#endif + {NULL, NULL}, }; diff --git a/src/blogc/sysinfo.c b/src/blogc/sysinfo.c index 191f60e..d934483 100644 --- a/src/blogc/sysinfo.c +++ b/src/blogc/sysinfo.c @@ -14,6 +14,10 @@ #include #endif /* HAVE_UNISTD_H */ +#ifdef HAVE_TIME_H +#include +#endif /* HAVE_TIME_H */ + #include #include "../common/utils.h" #include "sysinfo.h" @@ -45,3 +49,35 @@ blogc_sysinfo_inject_hostname(bc_trie_t *global) bc_trie_insert(global, "BLOGC_SYSINFO_HOSTNAME", hostname); } + + +char* +blogc_sysinfo_get_datetime(void) +{ +#ifndef HAVE_SYSINFO_DATETIME + return NULL; +#else + time_t tmp; + if (-1 == time(&tmp)) + return NULL; + + struct tm *t = gmtime(&tmp); + + char buf[1024]; + if (0 == strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", t)) + return NULL; + + return bc_strdup(buf); +#endif +} + + +void +blogc_sysinfo_inject_datetime(bc_trie_t *global) +{ + char *t = blogc_sysinfo_get_datetime(); + if (t == NULL) + return; + + bc_trie_insert(global, "BLOGC_SYSINFO_DATETIME", t); +} diff --git a/src/blogc/sysinfo.h b/src/blogc/sysinfo.h index 0104911..f26662a 100644 --- a/src/blogc/sysinfo.h +++ b/src/blogc/sysinfo.h @@ -19,9 +19,15 @@ #endif /* HAVE_GETHOSTNAME */ #endif /* HAVE_UNISTD_H */ +#ifdef HAVE_TIME_H +#define HAVE_SYSINFO_DATETIME 1 +#endif /* HAVE_TIME_H */ + #include "../common/utils.h" char* blogc_sysinfo_get_hostname(void); void blogc_sysinfo_inject_hostname(bc_trie_t *global); +char* blogc_sysinfo_get_datetime(void); +void blogc_sysinfo_inject_datetime(bc_trie_t *global); #endif /* ___SYSINFO_H */ -- cgit v1.2.3-18-g5258