summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--calendar.c44
1 files changed, 27 insertions, 17 deletions
diff --git a/calendar.c b/calendar.c
index 8011cb7..1cb953a 100644
--- a/calendar.c
+++ b/calendar.c
@@ -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;
}