summaryrefslogtreecommitdiff
path: root/appl/cmd/cal.b
blob: 90c4f77781a8a83ec6c14c230387fb18ce3e94cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
implement Cal;

#
# Copyright © 1995-2002 Lucent Technologies Inc.  All rights reserved.
# Limbo transliteration 2003 by Vita Nuova
# This software is subject to the Plan 9 Open Source Licence.
#

include "sys.m";
	sys: Sys;

include "draw.m";

include "bufio.m";
	bufio: Bufio;
	Iobuf: import bufio;

include "daytime.m";
	daytime: Daytime;
	Tm: import daytime;

Cal: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

dayw :=	" S  M Tu  W Th  F  S";
smon := array[] of {
	"January", "February", "March", "April",
	"May", "June", "July", "August",
	"September", "October", "November", "December",
};

mon := array[] of {
	0,
	31, 29, 31, 30,
	31, 30, 31, 31,
	30, 31, 30, 31,
};

bout: ref Iobuf;

init(nil: ref Draw->Context, args: list of string)
{
	y, m: int;

	sys = load Sys Sys->PATH;
	bufio = load Bufio Bufio->PATH;
	daytime = load Daytime Daytime->PATH;

	argc := len args;
	if(argc > 3){
		sys->fprint(sys->fildes(2), "usage: cal [month] [year]\n");
		raise "fail:usage";
	}
	bout = bufio->fopen(sys->fildes(1), Bufio->OWRITE);

#
# no arg, print current month
#
	if(argc <= 1) {
		m = curmo();
		y = curyr();
		return xshort(m, y);
	}
	args = tl args;

#
# one arg
#	if looks like a month, print month
#	else print year
#
	if(argc == 2) {
		y = number(hd args);
		if(y < 0)
			y = -y;
		if(y >= 1 && y <= 12)
			return xshort(y, curyr());
		return xlong(y);
	}

#
# two arg, month and year
#
	m = number(hd args);
	if(m < 0)
		m = -m;
	y = number(hd tl args);
	return xshort(m, y);
}

#
#	print out just month
#
xshort(m: int, y: int)
{
	if(m < 1 || m > 12)
		badarg();
	if(y < 1 || y > 9999)
		badarg();
	bout.puts(sys->sprint("   %s %ud\n", smon[m-1], y));
	bout.puts(sys->sprint("%s\n", dayw));
	lines := cal(m, y);
	for(i := 0; i < len lines; i++){
		bout.puts(lines[i]);
		bout.putc('\n');
	}
	bout.flush();
}

#
#	print out complete year
#
xlong(y: int)
{
	if(y<1 || y>9999)
		badarg();
	bout.puts("\n\n\n");
	bout.puts(sys->sprint("                                %ud\n", y));
	bout.putc('\n');
	months := array[3] of array of string;
	for(i:=0; i<12; i+=3) {
		bout.puts(sys->sprint("         %.3s", smon[i]));
		bout.puts(sys->sprint("                    %.3s", smon[i+1]));
		bout.puts(sys->sprint("                    %.3s\n", smon[i+2]));
		bout.puts(sys->sprint("%s   %s   %s\n", dayw, dayw, dayw));
		for(j := 0; j < 3; j++)
			months[j] = cal(i+j+1, y);
		for(l := 0; l < 6; l++){
			s := "";
			for(j = 0; j < 3; j++)
				s += sys->sprint("%-20.20s   ", months[j][l]);
			for(j = len s; j > 0 && s[j-1] == ' ';)
				j--;
			bout.puts(s[0:j]);
			bout.putc('\n');
		}
	}
	bout.flush();
}

badarg()
{
	sys->fprint(sys->fildes(2), "cal: bad argument\n");
	raise "fail:bad argument";
}

dict := array[] of {
	("january",	1),
	("february",	2),
	("march",	3),
	("april",	4),
	("may",		5),
	("june",		6),
	("july",		7),
	("august",	8),
	("sept",		9),
	("september",	9),
	("october",	10),
	("november",	11),
	("december",	12),
};

#
# convert to a number.
# if its a dictionary word,
# return negative  number
#
number(s: string): int
{
	if(len s >= 3){
		for(n:=0; n < len dict; n++){
			(word, val) := dict[n];
			if(s == word || s == word[0:3])
				return -val;
		}
	}
	n := 0;
	for(i := 0; i < len s; i++){
		c := s[i];
		if(c<'0' || c>'9')
			badarg();
		n = n*10 + c-'0';
	}
	return n;
}

pstr(str: string, n: int)
{
	bout.puts(sys->sprint("%-*.*s\n", n, n, str));
}

cal(m: int, y: int): array of string
{
	d := jan1(y);
	mon[9] = 30;

	case (jan1(y+1)+7-d)%7 {

	#
	#	non-leap year
	#
	1 =>
		mon[2] = 28;

	#
	#	leap year
	#
	2 =>
		mon[2] = 29;

	#
	#	1752
	#
	* =>
		mon[2] = 29;
		mon[9] = 19;
	}
	for(i:=1; i<m; i++)
		d += mon[i];
	d %= 7;
	lines := array[6] of string;
	l := 0;
	s := "";
	for(i = 0; i < d; i++)
		s += "   ";
	for(i=1; i<=mon[m]; i++) {
		if(i==3 && mon[m]==19) {
			i += 11;
			mon[m] += 11;
		}
		s += sys->sprint("%2d", i);
		if(++d == 7) {
			d = 0;
			lines[l++] = s;
			s = "";
		}else
			s[len s] = ' ';
	}
	if(s != nil){
		while(s[len s-1] == ' ')
			s = s[:len s-1];
		lines[l] = s;
	}
	return lines;
}

#
#	return day of the week
#	of jan 1 of given year
#
jan1(y: int): int
{
#
#	normal gregorian calendar
#	one extra day per four years
#

	d := 4+y+(y+3)/4;

#
#	julian calendar
#	regular gregorian
#	less three days per 400
#

	if(y > 1800) {
		d -= (y-1701)/100;
		d += (y-1601)/400;
	}

#
#	great calendar changeover instant
#

	if(y > 1752)
		d += 3;

	return d%7;
}

#
# get current month and year
#
curmo(): int
{
	tm := daytime->local(daytime->now());
	return tm.mon+1;
}

curyr(): int
{
	tm := daytime->local(daytime->now());
	return tm.year+1900;
}