This patch adds Nerd Font clock icons that change according to the
current hour (12-hour format). The icons are chosen based on the
hour string extracted via strftime(%I).

Changes:
- Added error checks for time() and localtime()
- Use tm->tm_hour directly instead of strftime+atoi
- Removed unnecessary <stdlib.h>
- Made icons array static
---
 components/datetime.c | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/components/datetime.c b/components/datetime.c
index 5b10daf..e7f0cb8 100644
--- a/components/datetime.c
+++ b/components/datetime.c
@@ -9,12 +9,41 @@ const char *
 datetime(const char *fmt)
 {
        time_t t;
+       struct tm *tm;
+       char hours[3];
+       char tmp[BUFSIZ];
+       const char *icon = "\0";
+       static const char *icons[] = { 
+               "󱐿 ", "󱑀 ", "󱑁 ", "󱑂 ",
+               "󱑃 ", "󱑄 ", "󱑅 ", "󱑆 ",
+               "󱑇 ", "󱑈 ", "󱑉 ", "󱑊 ",
+       };
+       int hour_int;
 
        t = time(NULL);
-       if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) {
+       if (t == (time_t)-1) {
+               warn("time: Failed to get current time");
+               return NULL;
+       }
+
+       tm = localtime(&t);
+       if (!tm) {
+               warn("localtime: Failed to convert time");
+               return NULL;
+       }
+
+       if (!strftime(tmp, sizeof(tmp), fmt, tm)) {
                warn("strftime: Result string exceeds buffer size");
                return NULL;
        }
 
-       return buf;
+       strftime(hours, sizeof(hours), "%I", tm);
+
+       hour_int = tm->tm_hour % 12;
+       if (hour_int == 0)
+               hour_int = 12;
+
+       icon = icons[hour_int - 1];
+
+       return bprintf("%s%s", icon, tmp);
 }
-- 
2.53.0


Reply via email to