diff options
author | Ori Bernstein <ori@eigenstate.org> | 2020-09-26 11:20:42 -0700 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2020-09-26 11:20:42 -0700 |
commit | d9f9e10e7b3ad61342ec1d9b034dd17f8452ccbc (patch) | |
tree | 3a0b155a8b2273cfe41110fe0a15ce50d6eb1e4f /sys/src/cmd/upas/imap4d | |
parent | 9afa5550f7497f84fb157ba07ff94301dbe06bcc (diff) |
upas: convert to tmdate, change timezone format
Complete the conversion of upas to remove ctime,
use the new date library, and print time zones
in +hhmm format, instead of NNN format.
This may affect code that expects specific names
for timezones. Fix that code.
Diffstat (limited to 'sys/src/cmd/upas/imap4d')
-rw-r--r-- | sys/src/cmd/upas/imap4d/date.c | 261 | ||||
-rw-r--r-- | sys/src/cmd/upas/imap4d/imap4d.c | 7 | ||||
-rw-r--r-- | sys/src/cmd/upas/imap4d/nlisttst.c | 2 | ||||
-rw-r--r-- | sys/src/cmd/upas/imap4d/print.c | 37 |
4 files changed, 42 insertions, 265 deletions
diff --git a/sys/src/cmd/upas/imap4d/date.c b/sys/src/cmd/upas/imap4d/date.c index 69900db4f..89cb36c08 100644 --- a/sys/src/cmd/upas/imap4d/date.c +++ b/sys/src/cmd/upas/imap4d/date.c @@ -1,142 +1,10 @@ #include "imap4d.h" -static char *wdayname[] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" -}; - -static char *monname[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - -/* - * zone : [A-Za-z][A-Za-z][A-Za-z] some time zone names - * | [A-IK-Z] military time; rfc1123 says the rfc822 spec is wrong. - * | "UT" universal time - * | [+-][0-9][0-9][0-9][0-9] - * zones is the rfc-822 list of time zone names - */ -static Namedint zones[] = -{ - {"A", -1 * 3600}, - {"B", -2 * 3600}, - {"C", -3 * 3600}, - {"CDT", -5 * 3600}, - {"CST", -6 * 3600}, - {"D", -4 * 3600}, - {"E", -5 * 3600}, - {"EDT", -4 * 3600}, - {"EST", -5 * 3600}, - {"F", -6 * 3600}, - {"G", -7 * 3600}, - {"GMT", 0}, - {"H", -8 * 3600}, - {"I", -9 * 3600}, - {"K", -10 * 3600}, - {"L", -11 * 3600}, - {"M", -12 * 3600}, - {"MDT", -6 * 3600}, - {"MST", -7 * 3600}, - {"N", +1 * 3600}, - {"O", +2 * 3600}, - {"P", +3 * 3600}, - {"PDT", -7 * 3600}, - {"PST", -8 * 3600}, - {"Q", +4 * 3600}, - {"R", +5 * 3600}, - {"S", +6 * 3600}, - {"T", +7 * 3600}, - {"U", +8 * 3600}, - {"UT", 0}, - {"V", +9 * 3600}, - {"W", +10 * 3600}, - {"X", +11 * 3600}, - {"Y", +12 * 3600}, - {"Z", 0}, -}; - -static void -zone2tm(Tm *tm, char *s) -{ - int i; - Tm aux, *atm; - - if(*s == '+' || *s == '-'){ - i = strtol(s, &s, 10); - tm->tzoff = (i/100)*3600 + i%100; - strncpy(tm->zone, "", 4); - return; - } - - /* - * look it up in the standard rfc822 table - */ - strncpy(tm->zone, s, 3); - tm->zone[3] = 0; - tm->tzoff = 0; - for(i = 0; i < nelem(zones); i++){ - if(cistrcmp(zones[i].name, s) == 0){ - tm->tzoff = zones[i].v; - return; - } - } - - /* - * one last try: look it up in the current local timezone - * probe a couple of times to get daylight/standard time change. - */ - aux = *tm; - memset(aux.zone, 0, 4); - aux.hour--; - for(i = 0; i < 2; i++){ - atm = localtime(tm2sec(&aux)); - if(cistrcmp(tm->zone, atm->zone) == 0){ - tm->tzoff = atm->tzoff; - return; - } - aux.hour++; - } - - strncpy(tm->zone, "GMT", 4); - tm->tzoff = 0; -} - -/* - * hh[:mm[:ss]] - */ -static void -time2tm(Tm *tm, char *s) -{ - tm->hour = strtoul(s, &s, 10); - if(*s++ != ':') - return; - tm->min = strtoul(s, &s, 10); - if(*s++ != ':') - return; - tm->sec = strtoul(s, &s, 10); -} - -static int -dateindex(char *d, char **tab, int n) -{ - int i; - - for(i = 0; i < n; i++) - if(cistrcmp(d, tab[i]) == 0) - return i; - return -1; -} - int imap4date(Tm *tm, char *date) { - char *flds[4]; - - if(getfields(date, flds, 3, 0, "-") != 3) + if(tmparse(tm, "DD-?MM-YYYY hh:mm:ss ?Z", date, nil, nil) == nil) return 0; - - tm->mday = strtol(flds[0], nil, 10); - tm->mon = dateindex(flds[1], monname, 12); - tm->year = strtol(flds[2], nil, 10) - 1900; return 1; } @@ -146,29 +14,17 @@ imap4date(Tm *tm, char *date) ulong imap4datetime(char *date) { - char *flds[4], *sflds[4]; - ulong t; Tm tm; - - if(getfields(date, flds, 4, 0, " ") != 3) - return ~0; - - if(!imap4date(&tm, flds[0])) - return ~0; - - if(getfields(flds[1], sflds, 3, 0, ":") != 3) - return ~0; - - tm.hour = strtol(sflds[0], nil, 10); - tm.min = strtol(sflds[1], nil, 10); - tm.sec = strtol(sflds[2], nil, 10); - - strcpy(tm.zone, "GMT"); - tm.yday = 0; - t = tm2sec(&tm); - zone2tm(&tm, flds[2]); - t -= tm.tzoff; - return t; + vlong s; + + s = -1; + if(tmparse(&tm, "?DD-?MM-YYYY hh:mm:ss ?Z", date, nil, nil) != nil) + s = tmnorm(&tm); + else if(tmparse(&tm, "?W, ?DD-?MM-YYYY hh:mm:ss ?Z", date, nil, nil) != nil) + s = tmnorm(&tm); + if(s > 0 && s < (1ULL<<31)) + return s; + return ~0; } /* @@ -181,85 +37,18 @@ imap4datetime(char *date) Tm* date2tm(Tm *tm, char *date) { - char *flds[7], *s, dstr[64]; - int n; - Tm gmt, *atm; - - /* - * default date is Thu Jan 1 00:00:00 GMT 1970 - */ - tm->wday = 4; - tm->mday = 1; - tm->mon = 1; - tm->hour = 0; - tm->min = 0; - tm->sec = 0; - tm->year = 70; - strcpy(tm->zone, "GMT"); - tm->tzoff = 0; - - strncpy(dstr, date, sizeof dstr); - dstr[sizeof dstr - 1] = 0; - n = tokenize(dstr, flds, 7); - if(n != 6 && n != 5) - return nil; - - if(n == 5){ - for(n = 5; n >= 1; n--) - flds[n] = flds[n - 1]; - n = 5; - }else{ - /* - * Wday[,] - */ - s = strchr(flds[0], ','); - if(s != nil) - *s = 0; - tm->wday = dateindex(flds[0], wdayname, 7); - if(tm->wday < 0) - return nil; - } - - /* - * check for the two major formats: - * Month first or day first - */ - tm->mon = dateindex(flds[1], monname, 12); - if(tm->mon >= 0){ - tm->mday = strtoul(flds[2], nil, 10); - time2tm(tm, flds[3]); - zone2tm(tm, flds[4]); - tm->year = strtoul(flds[5], nil, 10); - if(strlen(flds[5]) > 2) - tm->year -= 1900; - }else{ - tm->mday = strtoul(flds[1], nil, 10); - tm->mon = dateindex(flds[2], monname, 12); - if(tm->mon < 0) - return nil; - tm->year = strtoul(flds[3], nil, 10); - if(strlen(flds[3]) > 2) - tm->year -= 1900; - time2tm(tm, flds[4]); - zone2tm(tm, flds[5]); - } - - if(n == 5){ - gmt = *tm; - strncpy(gmt.zone, "", 4); - gmt.tzoff = 0; - atm = gmtime(tm2sec(&gmt)); - tm->wday = atm->wday; - }else{ - /* - * Wday[,] - */ - s = strchr(flds[0], ','); - if(s != nil) - *s = 0; - tm->wday = dateindex(flds[0], wdayname, 7); - if(tm->wday < 0) - return nil; - } - return tm; + char **f, *fmts[] = { + "?W, ?DD ?MMM YYYY hh:mm:ss ?Z", + "?W ?M ?DD hh:mm:ss ?Z YYYY", + "?W, DD-?MM-YY hh:mm:ss ?Z", + "?DD ?MMM YYYY hh:mm:ss ?Z", + "?M ?DD hh:mm:ss ?Z YYYY", + "DD-?MM-YYYY hh:mm:ss ?Z", + nil, + }; + + for(f = fmts; *f; f++) + if(tmparse(tm, *f, date, nil, nil) != nil) + return tm; + return nil; } diff --git a/sys/src/cmd/upas/imap4d/imap4d.c b/sys/src/cmd/upas/imap4d/imap4d.c index 3841bd3b0..061622fd8 100644 --- a/sys/src/cmd/upas/imap4d/imap4d.c +++ b/sys/src/cmd/upas/imap4d/imap4d.c @@ -215,6 +215,7 @@ main(int argc, char *argv[]) Binit(&bin, dup(0, -1), OREAD); close(0); Binit(&bout, 1, OWRITE); + tmfmtinstall(); quotefmtinstall(); fmtinstall('F', Ffmt); fmtinstall('D', Dfmt); /* rfc822; # imap date %Z */ @@ -501,6 +502,8 @@ appendcmd(char *tg, char *cmd) char *mbox, head[128]; uint t, n, now; int flags, ok; + Tzone *tz; + Tm tm; Uidplus u; mustbe(' '); @@ -536,7 +539,9 @@ appendcmd(char *tg, char *cmd) return; } - snprint(head, sizeof head, "From %s %s", username, ctime(t)); + tz = tzload("local"); + tmtime(&tm, t, tz); + snprint(head, sizeof head, "From %s %τ", username, tmfmt(&tm, "WW MMM _D hh:mm:ss Z YYYY")); ok = appendsave(mbox, flags, head, &bin, n, &u); crnl(); check(); diff --git a/sys/src/cmd/upas/imap4d/nlisttst.c b/sys/src/cmd/upas/imap4d/nlisttst.c index c6a8cbfbe..d168217e4 100644 --- a/sys/src/cmd/upas/imap4d/nlisttst.c +++ b/sys/src/cmd/upas/imap4d/nlisttst.c @@ -1,7 +1,5 @@ #include "nlist.c" -char username[] = "quanstro"; -char mboxdir[] = "/mail/box/quanstro/"; Biobuf bout; Bin *parsebin; diff --git a/sys/src/cmd/upas/imap4d/print.c b/sys/src/cmd/upas/imap4d/print.c index 80580ad14..2fd883d5a 100644 --- a/sys/src/cmd/upas/imap4d/print.c +++ b/sys/src/cmd/upas/imap4d/print.c @@ -90,40 +90,25 @@ Xfmt(Fmt *f) return fmtstrcpy(f, encfs(buf, sizeof buf, s)); } -static char *day[] = { - "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", -}; - -static char *mon[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - int Dfmt(Fmt *f) { - char buf[128], *p, *e, *sgn, *fmt; - int off; - Tm *tm; + char buf[128], *fmt; + Tm *tm, t; + Tzone *tz; tm = va_arg(f->args, Tm*); - if(tm == nil) - tm = localtime(time(0)); - sgn = "+"; - if(tm->tzoff < 0) - sgn = ""; - e = buf + sizeof buf; - p = buf; - off = (tm->tzoff/3600)*100 + (tm->tzoff/60)%60; + if(tm == nil){ + tz = tzload("local"); + tm = tmtime(&t, time(0), tz); + } if((f->flags & FmtSharp) == 0){ /* rfc822 style */ - fmt = "%.2d %s %.4d %.2d:%.2d:%.2d %s%.4d"; - p = seprint(p, e, "%s, ", day[tm->wday]); + fmt = "WW, DD MMM YYYY hh:mm:ss Z"; }else - fmt = "%2d-%s-%.4d %2.2d:%2.2d:%2.2d %s%4.4d"; - seprint(p, e, fmt, - tm->mday, mon[tm->mon], tm->year + 1900, tm->hour, tm->min, tm->sec, - sgn, off); + fmt = "DD-MMM-YYYY hh:mm:ss Z"; if(f->r == L'δ') - return fmtstrcpy(f, buf); + return fmtprint(f, "%τ", tmfmt(tm, fmt)); + snprint(buf, sizeof(buf), "%τ", tmfmt(tm, fmt)); return fmtprint(f, "%Z", buf); } |