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:
- Moved variable declaration to function top
---
components/datetime.c | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/components/datetime.c b/components/datetime.c
index 5b10daf..e810973 100644
--- a/components/datetime.c
+++ b/components/datetime.c
@@ -1,6 +1,7 @@
/* See LICENSE file for copyright and license details. */
#include <stdio.h>
#include <time.h>
+#include <stdlib.h>
#include "../slstatus.h"
#include "../util.h"
@@ -9,12 +10,30 @@ const char *
datetime(const char *fmt)
{
time_t t;
+ struct tm *tm;
+ char hours[3];
+ char tmp[BUFSIZ];
+ const char *icon = "\0";
+ const char *icons[] = {
+ " ", " ", " ", " ",
+ " ", " ", " ", " ",
+ " ", " ", " ", " ",
+ };
+ int hour_int;
t = time(NULL);
- if (!strftime(buf, sizeof(buf), fmt, localtime(&t))) {
+ tm = localtime(&t);
+
+ 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 = atoi(hours);
+ if (hour_int >= 1 && hour_int <= 12)
+ icon = icons[hour_int - 1];
+
+ return bprintf("%s%s", icon, tmp);
}
--
2.53.0