]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/Extras/libh/date.c
Updated documentation and comments about sendmail/pipe.
[nmh] / docs / historical / mh-jun-1982 / Extras / libh / date.c
1 #ifdef COMMENT
2 Proprietary Rand Corporation, 1981.
3 Further distribution of this software
4 subject to the terms of the Rand
5 license agreement.
6 #endif
7
8 /** Version of date.c in s1 modified to be a subroutine
9 * it is called with a string and returns a pointer to ints.
10 * ie, ip = date(string);
11 * returns 0 if it fails.
12 * only error message is bad conversion if string is illegal.
13 * The only legal format is ########[ps]
14 * where the int field is fixed input and 'p' forces PM by adding 12 hours
15 * and 's' would force standard savings time
16 */
17 int timbuf[2];
18 char *cbp;
19 int *cpx;
20 int dmsize[];
21 date(string)
22 char *string;
23 {
24 extern int timezone, *localtime();
25
26 cbp = string;
27 if(gtime()) {
28 write(2, "bad conversion\n", 15);
29 return(0);
30 }
31 if (*cbp != 's') {
32 /* convert to Greenwich time, on assumption of Standard time. */
33 dpadd(timbuf, timezone);
34 /* Now fix up to local daylight time. */
35 if ((cpx = localtime(timbuf))[8])
36 dpadd(timbuf, -1*60*60);
37 }
38 return(timbuf);
39 }
40
41 gtime()
42 {
43 register int i;
44 register int y, t;
45 int d, h, m;
46 extern int *localtime();
47 int nt[2];
48
49 t = gpair();
50 if(t<1 || t>12)
51 goto bad;
52 d = gpair();
53 if(d<1 || d>31)
54 goto bad;
55 h = gpair();
56 if(h == 24) {
57 h = 0;
58 d++;
59 }
60 m = gpair();
61 if(m<0 || m>59)
62 goto bad;
63 y = gpair();
64 if (y<0) {
65 time(nt);
66 y = localtime(nt)[5];
67 }
68 if (*cbp == 'p')
69 h =+ 12;
70 if (h<0 || h>23)
71 goto bad;
72 timbuf[0] = 0;
73 timbuf[1] = 0;
74 y =+ 1900;
75 for(i=1970; i<y; i++)
76 gdadd(dysize(i));
77 /* Leap year */
78 if (dysize(y)==366 && t >= 3)
79 gdadd(1);
80 while(--t)
81 gdadd(dmsize[t-1]);
82 gdadd(d-1);
83 gmdadd(24, h);
84 gmdadd(60, m);
85 gmdadd(60, 0);
86 return(0);
87
88 bad:
89 return(1);
90 }
91
92 gdadd(n)
93 {
94 register char *t;
95
96 t = timbuf[1]+n;
97 if(t < timbuf[1])
98 timbuf[0]++;
99 timbuf[1] = t;
100 }
101
102 gmdadd(m, n)
103 {
104 register int t1;
105
106 timbuf[0] =* m;
107 t1 = timbuf[1];
108 while(--m)
109 gdadd(t1);
110 gdadd(n);
111 }
112
113 gpair()
114 {
115 register int c, d;
116 register char *cp;
117
118 cp = cbp;
119 if(*cp == 0)
120 return(-1);
121 c = (*cp++ - '0') * 10;
122 if (c<0 || c>100)
123 return(-1);
124 if(*cp == 0)
125 return(-1);
126 if ((d = *cp++ - '0') < 0 || d > 9)
127 return(-1);
128 cbp = cp;
129 return (c+d);
130 }