ref: e702cfcafdc06ea23134bb44a99a90634d4f7d97
parent: 9d446410c5681235894f65d4e9c503fe4ad8b4a1
author: Ori Bernstein <[email protected]>
date: Sun Aug 9 15:46:38 EDT 2020
upas/fs: port date parsing to libc apis There was a lot of code in upas/fs to deal with dates. Now there isn't.
--- a/sys/src/cmd/upas/common/common.h
+++ b/sys/src/cmd/upas/common/common.h
@@ -52,9 +52,6 @@
void mailfmtinstall(void); /* 'U' = 2047fmt */
#pragma varargck type "U" char*
-/* totm.c */
-int fromtotm(char*, Tm*);
-
/* a pipe between parent and child*/
typedef struct{
Biobuf bb;
--- a/sys/src/cmd/upas/common/folder.c
+++ b/sys/src/cmd/upas/common/folder.c
@@ -1,5 +1,7 @@
#include "common.h"
+#define Ctimefmt "WW MMM _D hh:mm:ss ZZZ YYYY"
+
enum{
Mbox = 1,
Mdir,
@@ -185,7 +187,7 @@
appendfolder(Biobuf *b, char *addr, int fd)
{
char *s;
- int r;
+ int r, n;
Biobuf bin;
Folder *f;
Tm tm;
@@ -194,9 +196,10 @@
Bseek(f->out, 0, 2);
Binit(&bin, fd, OREAD);
s = Brdstr(&bin, '\n', 0);
- if(!s || strncmp(s, "From ", 5))
+ n = strlen(s);
+ if(!s || strncmp(s, "From ", 5) != 0)
Bprint(f->out, "From %s %.28s\n", addr, ctime(f->t));
- else if(fromtotm(s, &tm) >= 0)
+ else if(n > 5 && tmparse(&tm, Ctimefmt, s + 5, nil, nil) != nil)
f->t = tm2sec(&tm);
if(s)
Bwrite(f->out, s, strlen(s));
--- a/sys/src/cmd/upas/common/mkfile
+++ b/sys/src/cmd/upas/common/mkfile
@@ -10,7 +10,6 @@
fmt.$O\
libsys.$O\
process.$O\
- totm.$O\
HFILES=common.h\
sys.h\
--- a/sys/src/cmd/upas/common/totm.c
+++ /dev/null
@@ -1,36 +1,0 @@
-#include <common.h>
-
-static char mtab[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
-
-int
-ctimetotm(char *s, Tm *tm)
-{
- char buf[32];
-
- if(strlen(s) < 28)
- return -1;
- snprint(buf, sizeof buf, "%s", s);
- memset(tm, 0, sizeof *tm);
- buf[7] = 0;
- tm->mon = (strstr(mtab, buf+4) - mtab)/3;
- tm->mday = atoi(buf+8);
- tm->hour = atoi(buf+11);
- tm->min = atoi(buf+14);
- tm->sec = atoi(buf+17);
- tm->zone[0] = buf[20];
- tm->zone[1] = buf[21];
- tm->zone[2] = buf[22];
- tm->year = atoi(buf+24) - 1900;
- return 0;
-}
-
-int
-fromtotm(char *s, Tm *tm)
-{
- char buf[256], *f[3];
-
- snprint(buf, sizeof buf, "%s", s);
- if(getfields(buf, f, nelem(f), 0, " ") != 3)
- return -1;
- return ctimetotm(f[2], tm);
-}
--- a/sys/src/cmd/upas/fs/imap.c
+++ b/sys/src/cmd/upas/fs/imap.c
@@ -268,13 +268,10 @@
internaltounix(char *s)
{
Tm tm;
- if(strlen(s) < 20 || s[2] != '-' || s[6] != '-')
+
+ if(tmparse(&tm, "?DD-?MM-YYYY hh:mm:ss ?Z", s, nil, nil) == nil)
return -1;
- s[2] = ' ';
- s[6] = ' ';
- if(strtotm(s, &tm) == -1)
- return -1;
- return tm2sec(&tm);
+ return tmnorm(&tm);
}
static char*
@@ -981,7 +978,7 @@
}
if(c < 0){
/* new message */
- idprint(imap, "new: %U (%U)\n", f[i].uid, m? m->imapuid: 0);
+ idprint(imap, "new: %U (%U)\n", f[i].uid, m ? m->imapuid: 0);
if(f[i].sizes == 0 || f[i].sizes > Maxmsg){
idprint(imap, "skipping bad size: %lud\n", f[i].sizes);
i++;
--- a/sys/src/cmd/upas/fs/strtotm.c
+++ b/sys/src/cmd/upas/fs/strtotm.c
@@ -1,98 +1,27 @@
#include <u.h>
#include <libc.h>
-static char*
-skiptext(char *q)
-{
- while(*q != '\0' && *q != ' ' && *q != '\t' && *q != '\r' && *q != '\n')
- q++;
- return q;
-}
-
-static char*
-skipwhite(char *q)
-{
- while(*q == ' ' || *q == '\t' || *q == '\r' || *q == '\n')
- q++;
- return q;
-}
-
-static char* months[] = {
- "jan", "feb", "mar", "apr",
- "may", "jun", "jul", "aug",
- "sep", "oct", "nov", "dec"
-};
-
int
-strtotm(char *p, Tm *t)
+strtotm(char *s, Tm *t)
{
- char *q, *r;
- int j;
- Tm tm;
- int delta;
+ 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,
+ };
- delta = 0;
- memset(&tm, 0, sizeof(tm));
- tm.mon = -1;
- tm.hour = -1;
- tm.min = -1;
- tm.year = -1;
- tm.mday = -1;
- memcpy(tm.zone, "GMT", 3);
- for(p = skipwhite(p); *p; p = skipwhite(q)){
- q = skiptext(p);
-
- /* look for time in hh:mm[:ss] */
- if(r = memchr(p, ':', q - p)){
- tm.hour = strtol(p, 0, 10);
- tm.min = strtol(r + 1, 0, 10);
- if(r = memchr(r + 1, ':', q - (r + 1)))
- tm.sec = strtol(r + 1, 0, 10);
- else
- tm.sec = 0;
- continue;
- }
-
- /* look for month */
- for(j = 0; j < 12; j++)
- if(cistrncmp(p, months[j], 3) == 0){
- tm.mon = j;
- break;
- }
- if(j != 12)
- continue;
-
- /* look for time zone [A-Z][A-Z]T */
- if(q - p == 3)
- if(p[0] >= 'A' && p[0] <= 'Z')
- if(p[1] >= 'A' && p[1] <= 'Z')
- if(p[2] == 'T'){
- strecpy(tm.zone, tm.zone + 4, p);
- continue;
- }
-
- if(p[0] == '+'||p[0] == '-')
- if(q - p == 5 && strspn(p + 1, "0123456789") == 4){
- delta = (((p[1] - '0')*10 + p[2] - '0')*60 + (p[3] - '0')*10 + p[4] - '0')*60;
- if(p[0] == '-')
- delta = -delta;
- continue;
- }
- if(strspn(p, "0123456789") == q - p){
- j = strtol(p, nil, 10);
- if(j >= 1 && j <= 31)
- tm.mday = j;
- if(j >= 1900)
- tm.year = j - 1900;
- continue;
- }
- //eprint("strtotm: garbage %.*s\n", utfnlen(p, q - p), p);
- }
- if(tm.mon < 0 || tm.year < 0
- || tm.hour < 0 || tm.min < 0
- || tm.mday < 0)
- return -1;
-
- *t = *localtime(tm2sec(&tm) - delta);
- return 0;
+ for(f = fmt; *f; f++)
+ if(tmparse(t, *f, s, nil, nil) != nil)
+ return 0;
+ return -1;
}