|
|
1.1 root 1: /* $Header$ */
2:
3: #include <ctype.h>
4: #include "date.h"
5: #include "null.h"
6: #include "yesno.h"
7: /*
8: * date template character codes
9: */
10: #define L 1 /* a lower case chararacter */
11: #define U 2 /* an upper case character */
12: #define S 3 /* a space */
13: #define D 4 /* a digit */
14: #define O 5 /* an optional digit or space */
15: #define C 6 /* a colon */
16: #define N 7 /* a newline */
17: /*
18: * date templates
19: */
20: static char ctypes[] =
21: {U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,D,D,D,D,0};
22: static char tmztypes[] =
23: {U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,U,U,U,S,D,D,D,D,0};
24: static char *mon_tab[] =
25: {
26: "illegal month",
27: "Jan",
28: "Feb",
29: "Mar",
30: "Apr",
31: "May",
32: "Jun",
33: "Jul",
34: "Aug",
35: "Sep",
36: "Oct",
37: "Nov",
38: "Dec"
39: };
40: /*
41: * cmatch() matchs a date against a given template. Returns constant 1 if
42: * there is a match, otherwise 0.
43: */
44: static
45: cmatch(date, template)
46: register char *date; /* date string */
47: register char *template; /* template array */
48: {
49: register int c; /* comparison character */
50:
51: for (c = *date++; c != '\0' && *template != 0; c= *date++)
52: switch (*template++)
53: {
54: case L:
55: if (!islower(c))
56: return(0);
57: break;
58: case U:
59: if (!isupper(c))
60: return(0);
61: break;
62: case S:
63: if (c != ' ')
64: return(0);
65: break;
66: case D:
67: if (!isdigit(c))
68: return(0);
69: break;
70: case O:
71: if (c != ' ' && !isdigit(c))
72: return(0);
73: break;
74: case C:
75: if (c != ':')
76: return(0);
77: break;
78: case N:
79: if (c != '\n')
80: return(0);
81: break;
82: }
83: if (c != '\0' || *template != 0)
84: return(0);
85: return(1);
86: }
87:
88:
89:
90: /*
91: * isdate() tests if date is a ctime(3) generated date string.
92: * The ctypes template is used as the criterion of correctness. Also
93: * a possible trailing timezone is checked by the tmztype template.
94: * Returns constant 1 if valid, otherwise 0.
95: */
96: isdate(date)
97: char *date; /* date string */
98: {
99: int cmatch(); /* match date against template */
100:
101: if (cmatch(date, ctypes) == 1)
102: return(1);
103: return(cmatch(date, tmztypes));
104: }
105:
106:
107:
108: /*
109: * parsedate() breaks down the time returned by ctime(3) into a DATE
110: * struct. Returns constant NO if a bad date, otherwise YES.
111: */
112: parsedate(cdate, dt)
113: char *cdate; /* ctime(3) generated date string */
114: DATE *dt; /* pointer to broken-down time struct */
115: {
116: int atoi(); /* convert alpha string to integer */
117: int cmatch(); /* match date against template */
118: int strncmp(); /* compare n characters in string */
119:
120: if (cmatch(cdate, ctypes) == 1)
121: {
122: dt->t_year = atoi(cdate+20);
123: }
124: else if (cmatch(cdate, tmztypes) == 1)
125: {
126: dt->t_year = atoi(cdate+24);
127: }
128: else {
129: return(NO);
130: }
131: for (dt->t_mon = 12; dt->t_mon > 0; dt->t_mon--)
132: if (strncmp(mon_tab[dt->t_mon], cdate+4, 3) == 0)
133: break;
134: if (dt->t_mon <= 0)
135: return(NO);
136: dt->t_day = atoi(cdate+8);
137: dt->t_hour = atoi(cdate+11);
138: dt->t_min = atoi(cdate+14);
139: dt->t_sec = atoi(cdate+17);
140: return(YES);
141: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.