shithub: riscv

Download patch

ref: b7b077375fbb2cd99ca14b95545f111f47a30ca0
parent: 26ac80481d9e6e8abb359254f477bfad42fdc4ad
author: Ori Bernstein <[email protected]>
date: Thu Aug 27 06:32:02 EDT 2020

faces(1): remove bespoke date parser

In addition to being more code, this date parser would
treat local times as local, but anything that wasn't a
local time would get parsed as gmt, due to a quirk of
how tm2sec used to work.

This moves the code to tmparse, and fixes timezone parsing
at the same time.

--- a/sys/src/cmd/faces/plumb.c
+++ b/sys/src/cmd/faces/plumb.c
@@ -140,81 +140,42 @@
 	return buf;
 }
 
-static char* months[] = {
-	"jan", "feb", "mar", "apr",
-	"may", "jun", "jul", "aug", 
-	"sep", "oct", "nov", "dec"
-};
-
-static int
-getmon(char *s)
-{
-	int i;
-
-	for(i=0; i<nelem(months); i++)
-		if(cistrcmp(months[i], s) == 0)
-			return i;
-	return -1;
-}
-
-/* Fri Jul 23 14:05:14 EDT 1999 */
 ulong
-parsedatev(char **a)
+parsedate(char *s)
 {
-	char *p;
+	char **f, *fmt[] = {
+		"WW MMM DD hh:mm:ss ?Z YYYY",
+		"?WW ?DD ?MMM ?YYYY hh:mm:ss ?Z",
+		"?WW ?DD ?MMM ?YYYY hh:mm:ss",
+		"?WW, DD-?MM-YY",
+		"?DD ?MMM ?YYYY hh:mm:ss ?Z",
+		"?DD ?MMM ?YYYY hh:mm:ss",
+		"?DD-?MM-YY hh:mm:ss ?Z",
+		"?DD-?MM-YY hh:mm:ss",
+		"?DD-?MM-YY",
+		"?MMM/?DD/?YYYY hh:mm:ss ?Z",
+		"?MMM/?DD/?YYYY hh:mm:ss",
+		"?MMM/?DD/?YYYY",
+		nil,
+	};
 	Tm tm;
 
-	memset(&tm, 0, sizeof tm);
-	if((tm.mon=getmon(a[1])) == -1)
-		goto Err;
-	tm.mday = strtol(a[2], &p, 10);
-	if(*p != '\0')
-		goto Err;
-	tm.hour = strtol(a[3], &p, 10);
-	if(*p != ':')
-		goto Err;
-	tm.min = strtol(p+1, &p, 10);
-	if(*p != ':')
-		goto Err;
-	tm.sec = strtol(p+1, &p, 10);
-	if(*p != '\0')
-		goto Err;
-	if(strlen(a[4]) != 3)
-		goto Err;
-	strcpy(tm.zone, a[4]);
-	if(strlen(a[5]) != 4)
-		goto Err;
-	tm.year = strtol(a[5], &p, 10);
-	if(*p != '\0')
-		goto Err;
-	tm.year -= 1900;
-	return tm2sec(&tm);
-Err:
+	for(f = fmt; *f; f++)
+		if(tmparse(&tm, *f, s, nil, nil) != nil)
+			return tmnorm(&tm);
 	return time(0);
 }
 
-ulong
-parsedate(char *s)
-{
-	char *f[10];
-	int nf;
-
-	nf = getfields(s, f, nelem(f), 1, " ");
-	if(nf < 6)
-		return time(0);
-	return parsedatev(f);
-}
-
 /* achille Jul 23 14:05:15 delivered jmk From ms.com!bub Fri Jul 23 14:05:14 EDT 1999 (plan9.bell-labs.com!jmk) 1352 */
 /* achille Oct 26 13:45:42 remote local!rsc From rsc Sat Oct 26 13:45:41 EDT 2002 (rsc) 170 */
 int
 parselog(char *s, char **sender, ulong *xtime)
 {
-	char *f[20];
+	char *f[8];
 	int nf;
 
 	nf = getfields(s, f, nelem(f), 1, " ");
-	if(nf < 14)
+	if(nf < 8)
 		return 0;
 	if(strcmp(f[4], "delivered") == 0 && strcmp(f[5], user) == 0)
 		goto Found;
@@ -224,7 +185,7 @@
 
 Found:
 	*sender = estrdup(f[7]);
-	*xtime = parsedatev(&f[8]);
+	*xtime = parsedate(f[8]);
 	return 1;
 }