diff options
author | Jakob Kaivo <jkk@ung.org> | 2023-06-05 17:15:34 -0400 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2023-06-05 17:15:34 -0400 |
commit | 1873a4972f3a014c9b46d742b640f17c9ef4374b (patch) | |
tree | f7bd681255b6dfff491505211c5709331630c99c | |
parent | 372d85f621228680872a0e6f8be80836fbc7727f (diff) |
-rw-r--r-- | calendar.c | 44 |
1 files changed, 27 insertions, 17 deletions
@@ -26,9 +26,22 @@ #include <locale.h> #include <regex.h> #include <stdio.h> +#include <string.h> #include <time.h> #include <unistd.h> +static void optional_zero(size_t n, char buf[static n], char spec, struct tm *tm) +{ + char fmt[] = { '%', spec, '\0' }; + strftime(buf, n, fmt, tm); + if (buf[0] != '0') { + return; + } + + spec = buf[1]; + snprintf(buf, n, "0?%c", spec); +} + int main(int argc, char *argv[]) { setlocale(LC_ALL, ""); @@ -51,13 +64,13 @@ int main(int argc, char *argv[]) regex_t re[12] = { { 0 } }; time_t now = time(NULL); struct tm *tm = localtime(&now); - size_t nregs = 6; /* 3 for today, 3 for tomorrow */ + size_t nregs = 2; /* 1 for today, 1 for tomorrow */ switch (tm->tm_wday) { case 5: /* Friday, extend another day */ - nregs += 3; + nregs += 1; /* FALLTHRU */ case 6: /* Saturday, extend one day */ - nregs += 3; + nregs += 1; /* FALLTHRU */ default: break; @@ -65,22 +78,19 @@ int main(int argc, char *argv[]) const long SECONDS_PER_DAY = 60 * 60 * 24; const int REFLAGS = REG_ICASE | REG_NOSUB | REG_EXTENDED; - for (size_t i = 0; i < nregs; i += 3) { - char buf[64]; - strftime(buf, sizeof(buf), "%b.? *%e", tm); - if (regcomp(re + i, buf, REFLAGS) != 0) { - perror(buf); - return 1; - } + for (size_t i = 0; i < nregs; i += 1) { + char dom[4]; + optional_zero(sizeof(dom), dom, 'd', tm); - strftime(buf, sizeof(buf), "%B *%e", tm); - if (regcomp(re + i + 1, buf, REFLAGS) != 0) { - perror(buf); - return 1; - } + char mon[4]; + optional_zero(sizeof(mon), mon, 'm', tm); - strftime(buf, sizeof(buf), "%m/%d", tm); - if (regcomp(re + i + 2, buf, REFLAGS) != 0) { + char buf[64]; + strftime(buf, sizeof(buf), "((%b.?|%B) +|", tm); + strcat(buf, mon); + strcat(buf, "/)"); + strcat(buf, dom); + if (regcomp(re + i, buf, REFLAGS) != 0) { perror(buf); return 1; } |