|
|
1.1 root 1: /*
2: * PARTIME parse date/time string into a TM structure
3: *
4: * Usage:
5: * #include "time.h" -- expanded tm structure
6: * char *str; struct tm *tp;
7: * partime(str,tp);
8: * Returns:
9: * 0 if parsing failed
10: * else time values in specified TM structure (unspecified values
11: * set to TMNULL)
12: * Notes:
13: * This code is quasi-public; it may be used freely in like software.
14: * It is not to be sold, nor used in licensed software without
15: * permission of the author.
16: * For everyone's benefit, please report bugs and improvements!
17: * Copyright 1980 by Ken Harrenstien, SRI International.
18: * (ARPANET: KLH @ SRI)
19: */
20:
21: /* Hacknotes:
22: * If parsing changed so that no backup needed, could perhaps modify
23: * to use a FILE input stream. Need terminator, though.
24: * Perhaps should return 0 on success, else a non-zero error val?
25: * Flush AMPM from TM structure and handle locally within PARTIME,
26: * like midnight/noon?
27: */
28:
29: #ifndef lint
30: static char rcsid[]=
31: "$Header: /arthur/src/local/bin/rcs/src/RCS/partime.c,v 1.2 87/03/27 14:21:53 jenkins Exp $";
32: #endif
33:
34: /* $Log: partime.c,v $
35: * Revision 1.2 87/03/27 14:21:53 jenkins
36: * Port to suns
37: *
38: * Revision 1.1 84/01/23 14:50:07 kcs
39: * Initial revision
40: *
41: * Revision 1.1 82/05/06 11:38:26 wft
42: * Initial revision
43: *
44: */
45:
46: #include <stdio.h>
47: #include <ctype.h>
48: #include "time.h"
49:
50: #ifndef lint
51: static char timeid[] = TIMEID;
52: #endif
53:
54: struct tmwent {
55: char *went;
56: long wval; /* must be big enough to hold pointer or integer */
57: char wflgs;
58: char wtype;
59: };
60: /* wflgs */
61: #define TWSPEC 01 /* Word wants special processing */
62: #define TWTIME 02 /* Word is a time value (absence implies date) */
63: #define TWDST 04 /* Word is a DST-type timezone */
64: #define TW1200 010 /* Word is NOON or MIDNIGHT (sigh) */
65:
66: int pt12hack();
67: int ptnoise();
68: struct tmwent tmwords [] = {
69: {"january", 0, 0, TM_MON},
70: {"february", 1, 0, TM_MON},
71: {"march", 2, 0, TM_MON},
72: {"april", 3, 0, TM_MON},
73: {"may", 4, 0, TM_MON},
74: {"june", 5, 0, TM_MON},
75: {"july", 6, 0, TM_MON},
76: {"august", 7, 0, TM_MON},
77: {"september", 8, 0, TM_MON},
78: {"october", 9, 0, TM_MON},
79: {"november", 10, 0, TM_MON},
80: {"december", 11, 0, TM_MON},
81:
82: {"sunday", 0, 0, TM_WDAY},
83: {"monday", 1, 0, TM_WDAY},
84: {"tuesday", 2, 0, TM_WDAY},
85: {"wednesday", 3, 0, TM_WDAY},
86: {"thursday", 4, 0, TM_WDAY},
87: {"friday", 5, 0, TM_WDAY},
88: {"saturday", 6, 0, TM_WDAY},
89:
90: {"gmt", 0*60, TWTIME, TM_ZON}, /* Greenwich */
91: {"gst", 0*60, TWTIME, TM_ZON},
92: {"gdt", 0*60, TWTIME+TWDST, TM_ZON}, /* ?? */
93:
94: {"ast", 4*60, TWTIME, TM_ZON}, /* Atlantic */
95: {"est", 5*60, TWTIME, TM_ZON}, /* Eastern */
96: {"cst", 6*60, TWTIME, TM_ZON}, /* Central */
97: {"mst", 7*60, TWTIME, TM_ZON}, /* Mountain */
98: {"pst", 8*60, TWTIME, TM_ZON}, /* Pacific */
99: {"yst", 9*60, TWTIME, TM_ZON}, /* Yukon */
100: {"hst", 10*60, TWTIME, TM_ZON}, /* Hawaii */
101: {"bst", 11*60, TWTIME, TM_ZON}, /* Bering */
102:
103: {"adt", 4*60, TWTIME+TWDST, TM_ZON}, /* Atlantic */
104: {"edt", 5*60, TWTIME+TWDST, TM_ZON}, /* Eastern */
105: {"cdt", 6*60, TWTIME+TWDST, TM_ZON}, /* Central */
106: {"mdt", 7*60, TWTIME+TWDST, TM_ZON}, /* Mountain */
107: {"pdt", 8*60, TWTIME+TWDST, TM_ZON}, /* Pacific */
108: {"ydt", 9*60, TWTIME+TWDST, TM_ZON}, /* Yukon */
109: {"hdt", 10*60, TWTIME+TWDST, TM_ZON}, /* Hawaii */
110: {"bdt", 11*60, TWTIME+TWDST, TM_ZON}, /* Bering */
111:
112: {"daylight", 1, TWTIME+TWDST, TM_ZON}, /* Local Daylight */
113: {"standard", 1, TWTIME, TM_ZON}, /* Local Standard */
114: {"std", 1, TWTIME, TM_ZON}, /* " " */
115:
116: {"am", 1, TWTIME, TM_AMPM},
117: {"pm", 2, TWTIME, TM_AMPM},
118: {"noon", 12,TWTIME+TW1200, 0}, /* Special frobs */
119: {"midnight", 0, TWTIME+TW1200, 0},
120: {"at", (long)ptnoise, TWSPEC, 0}, /* Noise word */
121:
122: {0, 0, 0, 0}, /* Zero entry to terminate searches */
123: };
124:
125: #define TMWILD (-2) /* Value meaning item specified as wild-card */
126: /* (May use someday...) */
127:
128: struct token {
129: char *tcp; /* pointer to string */
130: int tcnt; /* # chars */
131: char tbrk; /* "break" char */
132: char tbrkl; /* last break char */
133: char tflg; /* 0 = alpha, 1 = numeric */
134: union { /* Resulting value; */
135: int tnum;/* either a #, or */
136: struct tmwent *ttmw;/* ptr to a tmwent. */
137: } tval;
138: };
139:
140: partime(astr, atm)
141: char *astr;
142: struct tm *atm;
143: { register int *tp;
144: register struct tmwent *twp;
145: register int i;
146: struct token btoken, atoken;
147: char *cp, ch;
148: int ord, midnoon;
149: int (*aproc)();
150:
151: tp = (int *)atm;
152: zaptime(tp); /* Initialize the TM structure */
153: midnoon = TMNULL; /* and our own temp stuff */
154: btoken.tcnt = btoken.tbrkl = 0;
155: btoken.tcp = astr;
156:
157: domore:
158: if(!ptitoken(btoken.tcp+btoken.tcnt,&btoken)) /* Get a token */
159: { if(btoken.tval.tnum) return(0); /* Read error? */
160: if(midnoon != TMNULL) /* EOF, wrap up */
161: return(pt12hack(tp, midnoon));
162: return(1); /* Win return! */
163: }
164: if(btoken.tflg == 0) /* Alpha? */
165: { twp = btoken.tval.ttmw; /* Yes, get ptr to entry */
166: if(twp->wflgs&TWSPEC) /* Special alpha crock */
167: { aproc = (int (*) ()) (twp->wval);
168: if(!(*aproc)(tp, twp, &btoken))
169: return(0); /* ERR: special word err */
170: goto domore;
171: }
172: if(twp->wflgs&TW1200)
173: if(ptstash(&midnoon,(int)twp->wval))
174: return(0); /* ERR: noon/midnite clash */
175: else goto domore;
176: if(ptstash(&tp[twp->wtype],(int)twp->wval))
177: return(0); /* ERR: val already set */
178: if(twp->wtype == TM_ZON) /* If was zone, hack DST */
179: if(ptstash(&tp[TM_ISDST],(twp->wflgs&TWDST)))
180: return(0); /* ERR: DST conflict */
181: goto domore;
182: }
183:
184: /* Token is number. Lots of hairy heuristics. */
185: if(btoken.tcnt >= 7) /* More than 6 digits in string? */
186: return(0); /* ERR: number too big */
187: if(btoken.tcnt == 6) /* 6 digits = HHMMSS. Needs special crock */
188: { /* since 6 digits are too big for integer! */
189: i = (btoken.tcp[0]-'0')*10 /* Gobble 1st 2 digits */
190: + btoken.tcp[1]-'0';
191: btoken.tcnt = 2; /* re-read last 4 chars */
192: goto coltime;
193: }
194:
195: i = btoken.tval.tnum; /* Value now known to be valid; get it. */
196: if( btoken.tcnt == 5 /* 5 digits = HMMSS */
197: || btoken.tcnt == 3) /* 3 digits = HMM */
198: { if(btoken.tcnt != 3)
199: if(ptstash(&tp[TM_SEC], i%100))
200: return(0); /* ERR: sec conflict */
201: else i /= 100;
202: hhmm4: if(ptstash(&tp[TM_MIN], i%100))
203: return(0); /* ERR: min conflict */
204: i /= 100;
205: hh2: if(ptstash(&tp[TM_HOUR], i))
206: return(0); /* ERR: hour conflict */
207: goto domore;
208: }
209:
210: if(btoken.tcnt == 4) /* 4 digits = YEAR or HHMM */
211: { if(tp[TM_YEAR] != TMNULL) goto hhmm4; /* Already got yr? */
212: if(tp[TM_HOUR] != TMNULL) goto year4; /* Already got hr? */
213: if((i%100) > 59) goto year4; /* MM >= 60? */
214: if(btoken.tbrk == ':') /* HHMM:SS ? */
215: if( ptstash(&tp[TM_HOUR],i/100)
216: || ptstash(&tp[TM_MIN], i%100))
217: return(0); /* ERR: hr/min clash */
218: else goto coltm2; /* Go handle SS */
219: if(btoken.tbrk != ',' && btoken.tbrk != '/'
220: && ptitoken(btoken.tcp+btoken.tcnt,&atoken) /* Peek */
221: && atoken.tflg == 0 /* alpha */
222: && (atoken.tval.ttmw->wflgs&TWTIME)) /* HHMM-ZON */
223: goto hhmm4;
224: if(btoken.tbrkl == '-' /* DD-Mon-YYYY */
225: || btoken.tbrkl == ',' /* Mon DD, YYYY */
226: || btoken.tbrkl == '/' /* MM/DD/YYYY */
227: || btoken.tbrkl == '.' /* DD.MM.YYYY */
228: || btoken.tbrk == '-' /* YYYY-MM-DD */
229: ) goto year4;
230: goto hhmm4; /* Give up, assume HHMM. */
231: }
232:
233: /* From this point on, assume tcnt == 1 or 2 */
234: /* 2 digits = YY, MM, DD, or HH (MM and SS caught at coltime) */
235: if(btoken.tbrk == ':') /* HH:MM[:SS] */
236: goto coltime; /* must be part of time. */
237: if(i > 31) goto yy2; /* If >= 32, only YY poss. */
238:
239: /* Check for numerical-format date */
240: for (cp = "/-."; ch = *cp++;)
241: { ord = (ch == '.' ? 0 : 1); /* n/m = D/M or M/D */
242: if(btoken.tbrk == ch) /* "NN-" */
243: { if(btoken.tbrkl != ch)
244: { if(ptitoken(btoken.tcp+btoken.tcnt,&atoken)
245: && atoken.tflg == 0
246: && atoken.tval.ttmw->wtype == TM_MON)
247: goto dd2;
248: if(ord)goto mm2; else goto dd2; /* "NN-" */
249: } /* "-NN-" */
250: if(tp[TM_DAY] == TMNULL
251: && tp[TM_YEAR] != TMNULL) /* If "YY-NN-" */
252: goto mm2; /* then always MM */
253: if(ord)goto dd2; else goto mm2;
254: }
255: if(btoken.tbrkl == ch /* "-NN" */
256: && tp[ord ? TM_MON : TM_DAY] != TMNULL)
257: if(tp[ord ? TM_DAY : TM_MON] == TMNULL) /* MM/DD */
258: if(ord)goto dd2; else goto mm2;
259: else goto yy2; /* "-YY" */
260: }
261:
262: /* At this point only YY, DD, and HH are left.
263: * YY is very unlikely since value is <= 32 and there was
264: * no numerical format date. Make one last try at YY
265: * before dropping through to DD vs HH code.
266: */
267: if(btoken.tcnt == 2 /* If 2 digits */
268: && tp[TM_HOUR] != TMNULL /* and already have hour */
269: && tp[TM_DAY] != TMNULL /* and day, but */
270: && tp[TM_YEAR] == TMNULL) /* no year, then assume */
271: goto yy2; /* that's what we have. */
272:
273: /* Now reduced to choice between HH and DD */
274: if(tp[TM_HOUR] != TMNULL) goto dd2; /* Have hour? Assume day. */
275: if(tp[TM_DAY] != TMNULL) goto hh2; /* Have day? Assume hour. */
276: if(i > 24) goto dd2; /* Impossible HH means DD */
277: if(!ptitoken(btoken.tcp+btoken.tcnt, &atoken)) /* Read ahead! */
278: if(atoken.tval.tnum) return(0); /* ERR: bad token */
279: else goto dd2; /* EOF, assume day. */
280: if( atoken.tflg == 0 /* If next token is an alpha */
281: && atoken.tval.ttmw->wflgs&TWTIME) /* time-spec, assume hour */
282: goto hh2; /* e.g. "3 PM", "11-EDT" */
283:
284: dd2: if(ptstash(&tp[TM_DAY],i)) /* Store day (1 based) */
285: return(0);
286: goto domore;
287:
288: mm2: if(ptstash(&tp[TM_MON], i-1)) /* Store month (make zero based) */
289: return(0);
290: goto domore;
291:
292: yy2: i += 1900;
293: year4: if(ptstash(&tp[TM_YEAR],i)) /* Store year (full number) */
294: return(0); /* ERR: year conflict */
295: goto domore;
296:
297: /* Hack HH:MM[[:]SS] */
298: coltime:
299: if(ptstash(&tp[TM_HOUR],i)) return(0);
300: if(!ptitoken(btoken.tcp+btoken.tcnt,&btoken))
301: return(!btoken.tval.tnum);
302: if(!btoken.tflg) return(0); /* ERR: HH:<alpha> */
303: if(btoken.tcnt == 4) /* MMSS */
304: if(ptstash(&tp[TM_MIN],btoken.tval.tnum/100)
305: || ptstash(&tp[TM_SEC],btoken.tval.tnum%100))
306: return(0);
307: else goto domore;
308: if(btoken.tcnt != 2
309: || ptstash(&tp[TM_MIN],btoken.tval.tnum))
310: return(0); /* ERR: MM bad */
311: if(btoken.tbrk != ':') goto domore; /* Seconds follow? */
312: coltm2: if(!ptitoken(btoken.tcp+btoken.tcnt,&btoken))
313: return(!btoken.tval.tnum);
314: if(!btoken.tflg || btoken.tcnt != 2 /* Verify SS */
315: || ptstash(&tp[TM_SEC], btoken.tval.tnum))
316: return(0); /* ERR: SS bad */
317: goto domore;
318: }
319:
320: /* Store date/time value, return 0 if successful.
321: * Fails if entry already set to a different value.
322: */
323: ptstash(adr,val)
324: int *adr;
325: { register int *a;
326: if( *(a=adr) != TMNULL)
327: return(*a != val);
328: *a = val;
329: return(0);
330: }
331:
332: /* This subroutine is invoked for NOON or MIDNIGHT when wrapping up
333: * just prior to returning from partime.
334: */
335: pt12hack(atp, aval)
336: int *atp, aval;
337: { register int *tp, i, h;
338: tp = atp;
339: if (((i=tp[TM_MIN]) && i != TMNULL) /* Ensure mins, secs */
340: || ((i=tp[TM_SEC]) && i != TMNULL)) /* are 0 or unspec'd */
341: return(0); /* ERR: MM:SS not 00:00 */
342: i = aval; /* Get 0 or 12 (midnite or noon) */
343: if ((h = tp[TM_HOUR]) == TMNULL /* If hour unspec'd, win */
344: || h == 12) /* or if 12:00 (matches either) */
345: tp[TM_HOUR] = i; /* Then set time */
346: else if(!(i == 0 /* Nope, but if midnight and */
347: &&(h == 0 || h == 24))) /* time matches, can pass. */
348: return(0); /* ERR: HH conflicts */
349: tp[TM_AMPM] = TMNULL; /* Always reset this value if won */
350: return(1);
351: }
352:
353: /* Null routine for no-op tokens */
354:
355: ptnoise() { return(1); }
356:
357: /* Get a token and identify it to some degree.
358: * Returns 0 on failure; token.tval will be 0 for normal EOF, otherwise
359: * hit error of some sort
360: */
361:
362: ptitoken(astr, tkp)
363: register struct token *tkp;
364: char *astr;
365: {
366: register char *cp;
367: register int i;
368:
369: tkp->tval.tnum = 0;
370: if(pttoken(astr,tkp) == 0)
371: #ifdef DEBUG
372: VOID printf("EOF\n");
373: #endif DEBUG
374: return(0);
375: cp = tkp->tcp;
376:
377: #ifdef DEBUG
378: i = cp[tkp->tcnt];
379: cp[tkp->tcnt] = 0;
380: VOID printf("Token: \"%s\" ",cp);
381: cp[tkp->tcnt] = i;
382: #endif DEBUG
383:
384: if(tkp->tflg)
385: for(i = tkp->tcnt; i > 0; i--)
386: tkp->tval.tnum = (int)tkp->tval.tnum*10 + ((*cp++)-'0');
387: else
388: { i = ptmatchstr(cp, tkp->tcnt, tmwords);
389: tkp->tval.tnum = i ? i : -1; /* Set -1 for error */
390:
391: #ifdef DEBUG
392: if(!i) VOID printf("Not found!\n");
393: #endif DEBUG
394:
395: if(!i) return(0);
396: }
397:
398: #ifdef DEBUG
399: if(tkp->tflg)
400: VOID printf("Val: %d.\n",tkp->tval.tnum);
401: else VOID printf("Found: \"%s\", val: %d., type %d\n",
402: tkp->tval.ttmw->went,tkp->tval.ttmw->wval,tkp->tval.ttmw->wtype);
403: #endif DEBUG
404:
405: return(1);
406: }
407:
408: /* Read token from input string into token structure */
409: pttoken(astr,tkp)
410: register struct token *tkp;
411: char *astr;
412: {
413: register char *cp;
414: register int c;
415:
416: tkp->tcp = cp = astr;
417: tkp->tbrkl = tkp->tbrk; /* Set "last break" */
418: tkp->tcnt = tkp->tbrk = tkp->tflg = 0;
419:
420: while(c = *cp++)
421: { switch(c)
422: { case ' ': case '\t': /* Flush all whitespace */
423: while((c = *cp++) && isspace(c));
424: cp--; /* Drop thru to handle brk */
425: case '(': case ')': /* Perhaps any non-alphanum */
426: case '-': case ',': /* shd qualify as break? */
427: case '/': case ':': case '.': /* Break chars */
428: if(tkp->tcnt == 0) /* If no token yet */
429: { tkp->tcp = cp; /* ignore the brk */
430: tkp->tbrkl = c;
431: continue; /* and go on. */
432: }
433: tkp->tbrk = c;
434: return(tkp->tcnt);
435: }
436: if(tkp->tcnt == 0) /* If first char of token, */
437: tkp->tflg = isdigit(c); /* determine type */
438: if(( isdigit(c) && tkp->tflg) /* If not first, make sure */
439: ||(!isdigit(c) && !tkp->tflg)) /* char matches type */
440: tkp->tcnt++; /* Win, add to token. */
441: else {
442: cp--; /* Wrong type, back up */
443: tkp->tbrk = c;
444: return(tkp->tcnt);
445: }
446: }
447: return(tkp->tcnt); /* When hit EOF */
448: }
449:
450:
451: ptmatchstr(astr,cnt,astruc)
452: char *astr;
453: int cnt;
454: struct tmwent *astruc;
455: { register char *cp, *mp;
456: register int c;
457: struct tmwent *lastptr;
458: struct integ { int word; }; /* For getting at array ptr */
459: int i;
460:
461: lastptr = 0;
462: for(;mp = (char *)((struct integ *)astruc)->word; astruc += 1)
463: { cp = astr;
464: for(i = cnt; i > 0; i--)
465: { switch((c = *cp++) ^ *mp++) /* XOR the chars */
466: { case 0: continue; /* Exact match */
467: case 040: if(isalpha(c))
468: continue;
469: }
470: break;
471: }
472: if(i==0)
473: if(*mp == 0) return((unsigned int)astruc); /* Exact match */
474: else if(lastptr) return(0); /* Ambiguous */
475: else lastptr = astruc; /* 1st ambig */
476: }
477: return((unsigned int)lastptr);
478: }
479:
480:
481:
482: zaptime(tp)
483: register int *tp;
484: /* clears tm structure pointed to by tp */
485: { register int i;
486: i = (sizeof (struct tm))/(sizeof (int));
487: do *tp++ = TMNULL; /* Set entry to "unspecified" */
488: while(--i); /* Faster than FOR */
489: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.