source: buchla-68k/orig/DATE/CT.C

Last change on this file was 3ae31e9, checked in by Thomas Lopatic <thomas@…>, 7 years ago

Imported original source code.

  • Property mode set to 100755
File size: 6.7 KB
Line 
1/*
2 =============================================================================
3 ct.c -- print a calendar with the current date and time for GEMDOS
4 Version 2 -- 1988-12-05 -- D.N. Lynx Crowe
5
6 Adapted from Public Domain source code from Usenet.
7 Original author unknown.
8
9 wkday() function and GEMDOS interface by D.N. Lynx Crowe.
10
11 Compile and link with the Atari Developer's Alcyon C package.
12 =============================================================================
13*/
14
15#include "osbind.h"
16#include "stddefs.h"
17
18char dayw[] = { " S M Tu W Th F S" }; /* calendar heading */
19
20char *sday[] = { /* days of the week */
21
22 "Sunday", "Monday", "Tuesday", "Wednesday",
23 "Thursday", "Friday", "Saturday"
24};
25
26char *smon[]= { /* months of the year */
27
28 "January", "February", "March", "April",
29 "May", "June", "July", "August",
30 "September", "October", "November", "December",
31};
32
33char mon[] = { /* days in a month */
34
35 0,
36 31, 29, 31, 30,
37 31, 30, 31, 31,
38 30, 31, 30, 31,
39};
40
41char string[432]; /* output work area */
42
43/*
44
45*/
46
47/*
48 =============================================================================
49 wkday() -- determine the day of the week by Zeller's Congruence
50
51 Uses the Gregorian calendar -- accurate from 1752 through 4901 A.D.
52
53 int
54 wkday(year, month, day)
55 int year, month, day;
56
57 year 1752..4901
58 month 1..12 1 = Jan., 2 = Feb., ..., 12 = Dec.
59 day 1..31
60
61 Returns:
62
63 0 - Sunday
64 1 - Monday
65 2 - Tuesday
66 3 - Wednesday
67 4 - Thursday
68 5 - Friday
69 6 - Saturday
70 =============================================================================
71*/
72
73int
74wkday(year, month, day)
75register int year, month, day;
76{
77 register int cen, yinc;
78
79 month -= 2;
80
81 if (month < 1) {
82
83 month += 12;
84 --year;
85 }
86
87 cen = year / 100;
88 yinc = year - (cen * 100);
89
90 return(((((26 * month) - 2) / 10) + day + yinc + (yinc / 4)
91 + (cen / 4) - (2 * cen)) % 7);
92}
93
94/*
95
96*/
97
98/*
99 =============================================================================
100 pstr(str, n) - print a string at a fixed length
101 =============================================================================
102*/
103
104pstr(str, n)
105char *str;
106int n;
107{
108 register int i;
109 register char *s;
110
111 s = str;
112 i = n;
113
114 while (i--)
115 if (*s++ EQ '\0')
116 s[-1] = ' ';
117
118 i = n + 1;
119
120 while (i--)
121 if (*--s NE ' ')
122 break;
123
124 s[1] = '\0';
125 printf("%s\n", str);
126}
127
128/*
129
130*/
131
132/*
133 =============================================================================
134 number(str) -- extract an integer from str
135 =============================================================================
136*/
137
138int
139number(str)
140char *str;
141{
142 register int n, c;
143 register char *s;
144
145 n = 0;
146 s = str;
147
148 while(c = *s++) {
149
150 if (c < '0' OR c > '9')
151 return(0);
152
153 n = (n * 10) + (c - '0');
154 }
155
156 return(n);
157}
158
159/*
160
161*/
162
163/*
164 =============================================================================
165 jan1(yr) -- return the day of the week for January 1st of a given year
166 =============================================================================
167 */
168
169int
170jan1(yr)
171int yr;
172{
173 register int y, d;
174
175 /* normal Gregorian calendar -- one extra day per four years */
176
177 y = yr;
178 d = 4 + y + (y + 3) / 4;
179
180 /* Julian calendar -- regular Gregorian less three days per 400 */
181
182 if (y > 1800) {
183
184 d -= (y - 1701) / 100;
185 d += (y - 1601) / 400;
186 }
187
188 /* great calendar changeover instant */
189
190 if (y > 1752)
191 d += 3;
192
193 return(d%7);
194}
195
196/*
197
198*/
199
200/*
201 =============================================================================
202 cal(m, y, p, w) -- calculate a calendar string
203 =============================================================================
204*/
205
206cal(m, y, p, w)
207int m, y;
208char *p;
209int w;
210{
211 register d, i;
212 register char *s;
213
214 s = p;
215 d = jan1(y);
216 mon[2] = 29;
217 mon[9] = 30;
218
219 switch((jan1(y + 1) + 7 - d) % 7) {
220
221 /*
222 * non-leap year
223 */
224 case 1:
225 mon[2] = 28;
226 break;
227
228 /*
229 * 1752
230 */
231 default:
232 mon[9] = 19;
233 break;
234
235 /*
236 * leap year
237 */
238 case 2:
239 ;
240 }
241
242 for (i = 1; i < m; i++)
243 d += mon[i];
244
245 d %= 7;
246 s += 3*d;
247
248 for (i = 1; i LE mon[m]; i++) {
249
250 if (i EQ 3 AND mon[m] EQ 19) {
251
252 i += 11;
253 mon[m] += 11;
254 }
255
256 if (i > 9)
257 *s = (i / 10) + '0';
258
259 s++;
260 *s++ = (i % 10) + '0';
261 s++;
262
263 if (++d EQ 7) {
264
265 d = 0;
266 s = p + w;
267 p = s;
268 }
269 }
270}
271
272/*
273
274*/
275
276/*
277 =============================================================================
278 main function for ct command
279 =============================================================================
280*/
281
282main(argc, argv)
283char *argv[];
284{
285 register int yr, i, j;
286 int mn, dy, hh, mm;
287 register long t;
288
289 t = Gettime();
290
291 yr = ((int)(t >> 25) & 0x007F) + 1980; /* year */
292 mn = (int)(t >> 21) & 0x000F; /* month */
293 dy = (int)(t >> 16) & 0x001F; /* day */
294
295 hh = (int)(t >> 11) & 0x001F; /* hour */
296 mm = (int)(t >> 5) & 0x003F; /* minute */
297
298 switch (argc) {
299
300 case 1:
301 break;
302
303 case 2:
304 goto allyr;
305
306 case 3:
307 mn = number(argv[1]);
308 yr = number(argv[2]);
309 break;
310
311 default:
312 printf("ct: Incorrect number of arguments\n\n");
313
314 printf("usage: ct [ [month] year ]\n\n");
315
316 printf(" month = 1..12 year = 1752..9999\n\n");
317
318 printf("It is %02d:%02d on %s, %s %d, %d.\n\n",
319 hh, mm, sday[wkday(yr, mn, dy)], smon[mn - 1], dy, yr);
320
321 exit(0);
322 }
323
324/*
325
326*/
327
328 /* print out just the current month */
329
330 if (mn < 1 OR mn > 12)
331 goto badarg;
332
333 if (yr < 1752 OR yr > 9999)
334 goto badarg;
335
336 printf("\n\n %s %u\n\n", smon[mn - 1], yr);
337 printf("%s\n", dayw);
338
339 cal(mn, yr, string, 24);
340
341 for (i = 0; i < 6 * 24; i += 24)
342 pstr(string + i, 24);
343
344 printf("It is %02d:%02d on %s, %s %d, %d.\n\n",
345 hh, mm, sday[wkday(yr, mn, dy)], smon[mn - 1], dy, yr);
346
347 exit(0);
348
349/*
350
351*/
352
353 /* print out complete year */
354
355allyr:
356 yr = number(argv[1]);
357
358 if (yr < 1752 OR yr > 9999)
359 goto badarg;
360
361 printf("\n %u\n", yr);
362 printf("\n");
363
364 for (i = 0; i < 12; i += 3) {
365
366 for (j = 0; j < 6 * 72; j++)
367 string[j] = '\0';
368
369 printf(" %.3s", smon[i]);
370 printf(" %.3s", smon[i + 1]);
371 printf(" %.3s\n", smon[i + 2]);
372 printf("%s %s %s\n", dayw, dayw, dayw);
373
374 cal(i + 1, yr, string, 72);
375 cal(i + 2, yr, string + 23, 72);
376 cal(i + 3, yr, string + 46, 72);
377
378 for (j = 0; j < 6 * 72; j += 72)
379 pstr(string + j, 72);
380 }
381
382 printf("\n");
383 exit(0);
384
385/*
386
387*/
388
389badarg:
390 printf("ct: Bad argument\n\n");
391
392 printf("usage: ct [ [month] year ]\n\n");
393 printf(" month = 1..12 year = 1752..9999\n\n");
394
395 printf("It is %02d:%02d on %s, %s %d, %d.\n\n\n",
396 hh, mm, sday[wkday(yr, mn, dy)], smon[mn - 1], dy, yr);
397
398 exit(1);
399}
Note: See TracBrowser for help on using the repository browser.