]> diplodocus.org Git - nmh/blob - docs/historical/mh-jun-1982/Extras/libh/gdate.c
Replaced use of snprintf() with memcpy()/memmove().
[nmh] / docs / historical / mh-jun-1982 / Extras / libh / gdate.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 char *tim_ptr;
9 int dmsize[];
10 int timezone;
11 int no_add;
12
13 char *gd_months[] {
14 "january", "february", "march", "april",
15 "may", "june", "july", "august",
16 "september", "october", "november", "december",
17 0,
18 };
19
20
21 long gdate(str)
22 {
23 long tim, now, ghours(), secs;
24 int day, month, year, *nowp;
25 register int i;
26 register char *monp;
27
28 time(&now);
29 nowp = localtime(&now);
30 tim_ptr = str;
31 if(*tim_ptr == '+') {
32 tim_ptr++;
33 return(now + ghours());
34 }
35 day = gd_gdec();
36 if(*tim_ptr == '-') {
37 tim_ptr++;
38 if(*tim_ptr >= 'A' && *tim_ptr <= 'z') {
39 monp = gd_head();
40 if((month = swmtch(monp, &gd_months)) < 0) {
41 printf("Month \"%s\" Nonexistant.\n", monp);
42 return(0l);
43 }
44 if(*tim_ptr == '-') {
45 tim_ptr++;
46 year = gd_gdec();
47 } else
48 year = nowp[5];
49 } else {
50 month = nowp[4];
51 year = nowp[5];
52 }
53 if(*tim_ptr == ' ' || *tim_ptr == '@') {
54 tim_ptr++;
55 secs = ghours();
56 } else
57 secs = nowp[0]+ nowp[1]*60l + nowp[2]*3600l;
58 tim = 0l;
59 if(year<1900) year =+ 1900;
60 for(i = 1970; i < year; i++)
61 tim =+ dysize(i);
62 if(dysize(year) == 366 && month >= 3)
63 tim++;
64 while(month--)
65 tim =+ dmsize[month];
66 tim =+ day - 1;
67 tim =* 24l*60l*60l;
68 tim =+ secs;
69 tim =+ timezone;
70 nowp = localtime(&tim);
71 if(nowp[8])
72 tim =- 1*60l*60l;
73 return(tim);
74 }
75 tim_ptr = str;
76 secs = ghours();
77 secs =- nowp[0] + nowp[1]*60l + nowp[2]*3600l;
78 if(secs < 0 && no_add == 0)
79 secs =+ 24 * 60l * 60l;
80 return(now + secs);
81 }
82
83
84 long ghours()
85 {
86 register char *cp;
87 register int h, m;
88 int s;
89
90 cp = tim_ptr;
91 s = m = 0;
92 h = gd_gdec();
93 if(tim_ptr - cp == 4 && *tim_ptr != ':') {
94 m = h % 100;
95 h = h / 100;
96 } else {
97 if(*tim_ptr == ':') {
98 tim_ptr++;
99 m = gd_gdec();
100 if(*tim_ptr == ':') {
101 tim_ptr++;
102 s = gd_gdec();
103 }
104 }
105 }
106 if(*tim_ptr == 'p') {
107 tim_ptr++;
108 h =+ 12;
109 }
110 return(h*3600l + m*60l + s);
111 }
112
113
114 gd_gdec()
115 {
116 register int c, i;
117
118 i = 0;
119 c = *tim_ptr;
120 while(c >= '0' && c <= '9') {
121 i = i*10 + c - '0';
122 c = *++tim_ptr;
123 }
124 return(i);
125 }
126
127
128 gd_head()
129 {
130 static char buf[20];
131 register char *c2, c;
132
133 c2 = buf;
134 while(((c = *tim_ptr) >= 'a' && c <= 'z') ||
135 (c >= 'A' && c <= 'Z')) {
136 if(c <= 'Z')
137 c =+ 'a' - 'A';
138 if(c2 < &buf[18])
139 *c2++ = c;
140 tim_ptr++;
141 }
142 *c2 = 0;
143 return(buf);
144 }