|
|
1.1 ! root 1: /* date.c */ ! 2: ! 3: #include "tws.h" ! 4: #include <stdio.h> ! 5: #include <strings.h> ! 6: #include <sys/time.h> ! 7: ! 8: char *dates[] = { ! 9: "11 MAR 1980 1559-PST", ! 10: "12 March 1980 11:41-EST", ! 11: "10 Feb 1982 11:22:30-PST", ! 12: "85/10/29 14:44:13", ! 13: "2 Dec 1986 1103-PST (Tuesday)", ! 14: "27-Dec-1986 2323", ! 15: "06-Oct-88 00:57", ! 16: "12/02/88 12:36:12 EST", ! 17: "Mon, 05 Dec 88 22:17:19 PST", ! 18: "Fri, 16 Dec 88 18:49:09 PST", ! 19: "5 Jan 89 11:45 +0100", ! 20: "Fri Jan 6 12:46:18 PST 1989", ! 21: "Tue Jan 24 07:41:26 1989 CST", ! 22: "2/3/89 6:33 AM", ! 23: "13 February 1989, 10:02:09 EST", ! 24: "Tue Feb 14 13:05:06 PST 1989", ! 25: "14 Feb 89 17:17:23 GMT", ! 26: "01 Apr 89 00:00:01 GMT", ! 27: "10 Jul 89 15:24", ! 28: "Tue Nov 28 16:31:28 1989", ! 29: "Wed Nov 29 14:54:37 -0800 1989", ! 30: "Wed, 22 Nov 89 22:29:04 JST", ! 31: 0 ! 32: }; ! 33: ! 34: static int lflag = 0; /* long output, decode tw_flags */ ! 35: static int tflag = 0; /* run test on standard test dates */ ! 36: static int bflag = 0; /* run benchmark on standard test dates */ ! 37: ! 38: static char *argv0 = "?"; ! 39: ! 40: /* ARGSUSED */ ! 41: void ! 42: main (argc, argv) ! 43: int argc; ! 44: char **argv; ! 45: { ! 46: register char *cp, **p; ! 47: FILE *f = stdin; ! 48: char buf[1024]; ! 49: ! 50: if (argv0 = rindex(argv[0], '/')) ! 51: ++argv0; ! 52: else ! 53: argv0 = argv[0]; ! 54: argc--; ! 55: argv++; ! 56: while (argc > 0 && *argv[0] == '-') { ! 57: if (strcmp("-l", argv[0]) == 0) ! 58: lflag = 1; ! 59: else if (strcmp("-t", argv[0]) == 0) ! 60: tflag = 1; ! 61: else if (strcmp("-b", argv[0]) == 0) ! 62: bflag = 1; ! 63: else ! 64: break; /* force "usage" error */ ! 65: argc--; ! 66: argv++; ! 67: } ! 68: if (argc > 1) { ! 69: fprintf(stderr, "usage: %s [-l] [file]\n", argv0); ! 70: exit(1); ! 71: } ! 72: if (argc == 1 && (f = fopen(argv[0], "r")) == 0) { ! 73: perror(argv[0]); ! 74: exit(1); ! 75: } ! 76: if (bflag) { ! 77: /* Benchmark standard dates */ ! 78: dobench(); ! 79: exit(0); ! 80: } ! 81: if (tflag) { ! 82: /* Test standard dates */ ! 83: for (p = dates; *p; p++) ! 84: doit(*p); ! 85: exit(0); ! 86: } ! 87: while (fgets(buf, sizeof(buf), f)) { ! 88: cp = buf + strlen(buf) - 1; ! 89: if (cp >= buf && *cp == '\n') ! 90: *cp = '\0'; ! 91: doit(buf); ! 92: } ! 93: exit (0); ! 94: } ! 95: ! 96: doit(buf) ! 97: register char *buf; ! 98: { ! 99: register char *cp, *d; ! 100: register struct tws *t; ! 101: register int i; ! 102: ! 103: t = dparsetime(buf); ! 104: i = 30; ! 105: if (t) { ! 106: d = dasctime(t); ! 107: if ((t->tw_flags & TW_SZONE) == TW_SZNIL) ! 108: i -= 4; ! 109: else if (cp = rindex(d, ' ')) ! 110: i -= 4 - strlen(cp); ! 111: } else ! 112: d = "<couldn't parse>"; ! 113: printf ("%*s\t\"%s\"", i, d, buf); ! 114: if (lflag && t) { ! 115: i = 0; ! 116: if ((t->tw_flags & TW_SDAY) == TW_SEXP) { ! 117: printf(" (E-day"); ! 118: i++; ! 119: } ! 120: if ((t->tw_flags & TW_SDAY) == TW_SIMP) { ! 121: if (i == 0) ! 122: printf(" ("); ! 123: else if (i > 0) ! 124: printf(", "); ! 125: printf("I-day"); ! 126: i++; ! 127: } ! 128: if ((t->tw_flags & TW_SZONE) == TW_SZEXP) { ! 129: if (i == 0) ! 130: printf(" ("); ! 131: else if (i > 0) ! 132: printf(", "); ! 133: printf("E-zone"); ! 134: i++; ! 135: } ! 136: if (i > 0) ! 137: putchar(')'); ! 138: } ! 139: putchar('\n'); ! 140: } ! 141: ! 142: dobench() ! 143: { ! 144: register char **p; ! 145: register int n, i; ! 146: struct timeval t1, t2; ! 147: long ms; ! 148: ! 149: /* Pagefault things in (this may make timings more repeatable) */ ! 150: for (p = dates; *p; p++) ! 151: (void) dparsetime(*p); ! 152: ! 153: /* Bench-ho! */ ! 154: n = 0; ! 155: gettimeofday(&t1, 0); ! 156: for (i = 1000; i > 0; --i) ! 157: for (p = dates; *p; p++) { ! 158: ++n; ! 159: if (dparsetime(*p) == 0) { ! 160: fprintf(stderr, "%s: Lose (parse %d)\n", ! 161: argv0, n); ! 162: exit(1); ! 163: } ! 164: } ! 165: gettimeofday(&t2, 0); ! 166: ms = ((t2.tv_sec * 1000) + t2.tv_usec / 1000) - ! 167: ((t1.tv_sec * 1000) + t1.tv_usec / 1000); ! 168: printf("%s: %d parses took %.2f seconds (%.2f per second)\n", ! 169: argv0, n, (float)ms / 1000, (float)(n * 1000) / ms); ! 170: } ! 171: ! 172: /* XXX need to use varargs */ ! 173: void ! 174: adios(what, fmt, a, b, c, d, e, f) ! 175: char *what, *fmt, *a, *b, *c, *d, *e, *f; ! 176: { ! 177: if (what) ! 178: fprintf(stderr, "%s: ", what); ! 179: fprintf(stderr, fmt, a, b, c, d, e, f); ! 180: exit(1); ! 181: } ! 182:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.