summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2022-08-15 20:39:16 -0400
committerJune McEnroe <june@causal.agency>2022-08-15 20:39:16 -0400
commit5e7db40831a25b64499f78fa952a34e5a1d56b39 (patch)
tree4836411245139def57f01eb372eb5bc78d3cacdf
parentAdd named dates to when (diff)
downloadsrc-5e7db40831a25b64499f78fa952a34e5a1d56b39.tar.gz
src-5e7db40831a25b64499f78fa952a34e5a1d56b39.zip
Allow names with prefixes of months and days
-rw-r--r--bin/man1/when.16
-rw-r--r--bin/when.y38
2 files changed, 23 insertions, 21 deletions
diff --git a/bin/man1/when.1 b/bin/man1/when.1
index f955daf4..205b2d81 100644
--- a/bin/man1/when.1
+++ b/bin/man1/when.1
@@ -33,14 +33,12 @@ Names are alphanumeric including underscores.
 .It Ar month Ar date Op Ar year
 A full date,
 or a date in the current year.
-.Ar month
-must be at least three letters.
+Months can be abbreviated to three letters.
 .
 .It Ar day
 A day of the week
 in the current week.
-.Ar day
-must be at least three letters.
+Days can be abbreviated to three letters.
 .
 .It Sy < Ar date
 The date one week before.
diff --git a/bin/when.y b/bin/when.y
index 095493d7..83b9efdc 100644
--- a/bin/when.y
+++ b/bin/when.y
@@ -30,12 +30,13 @@ static int yylex(void);
 #define YYSTYPE struct tm
 
 static const char *Days[7] = {
-	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
+	"Sunday", "Monday", "Tuesday", "Wednesday",
+	"Thursday", "Friday", "Saturday",
 };
 
 static const char *Months[12] = {
-	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
+	"January", "February", "March", "April", "May", "June",
+	"July", "August", "September", "October", "November", "December",
 };
 
 static const struct tm Week = { .tm_mday = 7 };
@@ -163,7 +164,7 @@ static void setDate(const char *name, struct tm date) {
 
 static void printDate(struct tm date) {
 	printf(
-		"%s %s %d %d\n",
+		"%.3s %.3s %d %d\n",
 		Days[date.tm_wday], Months[date.tm_mon],
 		date.tm_mday, 1900 + date.tm_year
 	);
@@ -256,22 +257,25 @@ static int yylex(void) {
 		return Number;
 	}
 
-	for (int i = 0; i < 7; ++i) {
-		if (strncasecmp(input, Days[i], 3)) continue;
-		while (isalpha(*input)) input++;
-		yylval.tm_wday = i;
-		return Day;
-	}
+	size_t len;
+	for (len = 0; isalnum(input[len]) || input[len] == '_'; ++len);
+
+	if (len >= 3) {
+		for (int i = 0; i < 7; ++i) {
+			if (strncasecmp(input, Days[i], len)) continue;
+			yylval.tm_wday = i;
+			input += len;
+			return Day;
+		}
 
-	for (int i = 0; i < 12; ++i) {
-		if (strncasecmp(input, Months[i], 3)) continue;
-		while (isalpha(*input)) input++;
-		yylval.tm_mon = i;
-		return Month;
+		for (int i = 0; i < 12; ++i) {
+			if (strncasecmp(input, Months[i], len)) continue;
+			yylval.tm_mon = i;
+			input += len;
+			return Month;
+		}
 	}
 
-	size_t len;
-	for (len = 0; isalnum(input[len]) || input[len] == '_'; ++len);
 	if (len && (len != 1 || !strchr("dwmy", *input))) {
 		yylval.tm_zone = strndup(input, len);
 		if (!yylval.tm_zone) err(EX_OSERR, "strndup");