ncal.c revision 32282
131744Shelbig/*-
231744Shelbig * Copyright (c) 1997 Wolfgang Helbig
331744Shelbig * All rights reserved.
431744Shelbig *
531744Shelbig * Redistribution and use in source and binary forms, with or without
631744Shelbig * modification, are permitted provided that the following conditions
731744Shelbig * are met:
831744Shelbig * 1. Redistributions of source code must retain the above copyright
931744Shelbig *    notice, this list of conditions and the following disclaimer.
1031744Shelbig * 2. Redistributions in binary form must reproduce the above copyright
1131744Shelbig *    notice, this list of conditions and the following disclaimer in the
1231744Shelbig *    documentation and/or other materials provided with the distribution.
1331744Shelbig *
1431744Shelbig * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1531744Shelbig * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1631744Shelbig * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1731744Shelbig * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1831744Shelbig * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1931744Shelbig * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2031744Shelbig * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2131744Shelbig * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2231744Shelbig * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2331744Shelbig * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2431744Shelbig * SUCH DAMAGE.
2531744Shelbig *
2632282Shelbig *	$Id: ncal.c,v 1.3 1998/01/03 15:10:11 helbig Exp $
2731744Shelbig */
2831744Shelbig#include <calendar.h>
2931744Shelbig#include <err.h>
3031744Shelbig#include <locale.h>
3131744Shelbig#include <stdio.h>
3231744Shelbig#include <stdlib.h>
3331744Shelbig#include <string.h>
3431744Shelbig#include <sysexits.h>
3531744Shelbig#include <time.h>
3631744Shelbig#include <unistd.h>
3731744Shelbig
3831744Shelbig/* Width of one month with backward compatibility */
3931744Shelbig#define MONTH_WIDTH_B_J 27
4031744Shelbig#define MONTH_WIDTH_B 20
4131744Shelbig
4231744Shelbig#define MONTH_WIDTH_J 24
4331744Shelbig#define MONTH_WIDTH 18
4431744Shelbig
4531744Shelbig#define MAX_WIDTH 28
4631744Shelbig
4731744Shelbigtypedef struct date date;
4831744Shelbig
4931744Shelbigstruct monthlines {
5031744Shelbig	char name[MAX_WIDTH + 1];
5131744Shelbig	char lines[7][MAX_WIDTH + 1];
5231744Shelbig	char weeks[MAX_WIDTH + 1];
5331744Shelbig};
5431744Shelbig
5531744Shelbigstruct weekdays {
5631744Shelbig	char names[7][4];
5731744Shelbig};
5831744Shelbig
5931744Shelbig/* The switches from Julian to Gregorian in some countries */
6031744Shelbigstatic struct djswitch {
6131744Shelbig	char *cc;	/* Country code according to ISO 3166 */
6231744Shelbig	char *nm;	/* Name of country */
6331744Shelbig	date dt;	/* Last day of Julian calendar */
6432215Shelbig	char *efmt;	/* strftime format for printing date of easter */
6531744Shelbig} switches[] = {
6632215Shelbig	{"AL", "Albania",	{1912, 11, 30}, "%e %B %Y"},
6732282Shelbig	{"AT", "Austria",	{1583, 10,  5}, "%e. %B %Y"},
6832215Shelbig	{"AU", "Australia",	{1752,  9,  2}, "%B %e, %Y"},
6932282Shelbig	{"BE", "Belgium",	{1582, 12, 14}, "%B %e, %Y"},
7032215Shelbig	{"BG", "Bulgaria",	{1916,  3, 18}, "%e %B %Y"},
7132215Shelbig	{"CA", "Canada",	{1752,  9,  2}, "%B %e, %Y"},
7232215Shelbig	{"CH", "Switzerland",	{1655,  2, 28}, "%e. %B %Y"},
7332215Shelbig	{"CN", "China",		{1911, 12, 18}, "%e %B %Y"},
7432215Shelbig	{"CZ", "Czech Republic",{1584,  1,  6}, "%e %B %Y"},
7532215Shelbig	{"DE", "Germany",	{1700,  2, 18}, "%e. %B %Y"},
7632215Shelbig	{"DK", "Denmark",	{1700,  2, 18}, "%e. %B %Y"},
7732215Shelbig	{"ES", "Spain",		{1582, 10,  4}, "%e de %B de %Y"},
7832282Shelbig	{"FI", "Finland",	{1753,  2, 17}, "%e %B %Y"},
7932215Shelbig	{"FR", "France",	{1582, 12,  9}, "%e. %B %Y"},
8032215Shelbig	{"GB", "United Kingdom",{1752,  9,  2}, "%e %B %Y"},
8132215Shelbig	{"GR", "Greece",	{1924,  3,  9}, "%e %B %Y"},
8232215Shelbig	{"HU", "Hungary",	{1587, 10, 21}, "%e %B %Y"},
8332215Shelbig	{"IS", "Iceland",	{1700, 11, 16}, "%e %B %Y"},
8432215Shelbig	{"IT", "Italy",		{1582, 10,  4}, "%e %B %Y"},
8532215Shelbig	{"JP", "Japan",		{1918, 12, 18}, "%Y\x94N %B%e"},
8632282Shelbig	{"LI", "Lithuania",	{1918,  2,  1}, "%e %B %Y"},
8732282Shelbig	{"LN", "Latin",		{9999, 31, 12}, "%e %B %Y"},
8832282Shelbig	{"LU", "Luxembourg",	{1582, 12, 14}, "%e %B %Y"},
8932215Shelbig	{"LV", "Latvia",	{1918,  2,  1}, "%e %B %Y"},
9032282Shelbig	{"NL", "Netherlands",	{1582, 12, 14}, "%e %B %Y"},
9132215Shelbig	{"NO", "Norway",	{1700,  2, 18}, "%e %B %Y"},
9232215Shelbig	{"PL", "Poland",	{1582, 10,  4}, "%e %B %Y"},
9332215Shelbig	{"PT", "Portugal",	{1582, 10,  4}, "%e %B %Y"},
9432282Shelbig	{"RO", "Romania",	{1919,  3, 31}, "%e %B %Y"},
9532282Shelbig	{"RU", "Russia",	{1918,  1, 31}, "%e %B %Y"},
9632215Shelbig	{"SI", "Slovenia",	{1919,  3,  4}, "%e %B %Y"},
9732215Shelbig	{"SU", "USSR",		{1920,  3,  4}, "%e %B %Y"},
9832215Shelbig	{"SW", "Sweden",	{1753,  2, 17}, "%e %B %Y"},
9932215Shelbig	{"TR", "Turkey",	{1926, 12, 18}, "%e %B %Y"},
10032215Shelbig	{"US", "United States",	{1752,  9,  2}, "%B %e, %Y"},
10132215Shelbig	{"YU", "Yugoslavia",	{1919,  3,  4}, "%e %B %Y"}
10231744Shelbig};
10331744Shelbig
10432215Shelbigstruct djswitch *dftswitch =
10532215Shelbig    switches + sizeof(switches) / sizeof(struct djswitch) - 2;
10632215Shelbig    /* default switch (should be "US") */
10732215Shelbig
10831744Shelbig/* Table used to print day of month and week numbers */
10931744Shelbigchar daystr[] = "     1  2  3  4  5  6  7  8  9 10 11 12 13 14 15"
11031744Shelbig		" 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
11131744Shelbig		" 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
11231744Shelbig		" 48 49 50 51 52 53";
11331744Shelbig
11431744Shelbig/* Table used to print day of year and week numbers */
11531744Shelbigchar jdaystr[] = "       1   2   3   4   5   6   7   8   9"
11631744Shelbig		 "  10  11  12  13  14  15  16  17  18  19"
11731744Shelbig		 "  20  21  22  23  24  25  26  27  28  29"
11831744Shelbig		 "  30  31  32  33  34  35  36  37  38  39"
11931744Shelbig		 "  40  41  42  43  44  45  46  47  48  49"
12031744Shelbig		 "  50  51  52  53  54  55  56  57  58  59"
12131744Shelbig		 "  60  61  62  63  64  65  66  67  68  69"
12231744Shelbig		 "  70  71  72  73  74  75  76  77  78  79"
12331744Shelbig		 "  80  81  82  83  84  85  86  87  88  89"
12431744Shelbig		 "  90  91  92  93  94  95  96  97  98  99"
12531744Shelbig		 " 100 101 102 103 104 105 106 107 108 109"
12631744Shelbig		 " 110 111 112 113 114 115 116 117 118 119"
12731744Shelbig		 " 120 121 122 123 124 125 126 127 128 129"
12831744Shelbig		 " 130 131 132 133 134 135 136 137 138 139"
12931744Shelbig		 " 140 141 142 143 144 145 146 147 148 149"
13031744Shelbig		 " 150 151 152 153 154 155 156 157 158 159"
13131744Shelbig		 " 160 161 162 163 164 165 166 167 168 169"
13231744Shelbig		 " 170 171 172 173 174 175 176 177 178 179"
13331744Shelbig		 " 180 181 182 183 184 185 186 187 188 189"
13431744Shelbig		 " 190 191 192 193 194 195 196 197 198 199"
13531744Shelbig		 " 200 201 202 203 204 205 206 207 208 209"
13631744Shelbig		 " 210 211 212 213 214 215 216 217 218 219"
13731744Shelbig		 " 220 221 222 223 224 225 226 227 228 229"
13831744Shelbig		 " 230 231 232 233 234 235 236 237 238 239"
13931744Shelbig		 " 240 241 242 243 244 245 246 247 248 249"
14031744Shelbig		 " 250 251 252 253 254 255 256 257 258 259"
14131744Shelbig		 " 260 261 262 263 264 265 266 267 268 269"
14231744Shelbig		 " 270 271 272 273 274 275 276 277 278 279"
14331744Shelbig		 " 280 281 282 283 284 285 286 287 288 289"
14431744Shelbig		 " 290 291 292 293 294 295 296 297 298 299"
14531744Shelbig		 " 300 301 302 303 304 305 306 307 308 309"
14631744Shelbig		 " 310 311 312 313 314 315 316 317 318 319"
14731744Shelbig		 " 320 321 322 323 324 325 326 327 328 329"
14831744Shelbig		 " 330 331 332 333 334 335 336 337 338 339"
14931744Shelbig		 " 340 341 342 343 344 345 346 347 348 349"
15031744Shelbig		 " 350 351 352 353 354 355 356 357 358 359"
15131744Shelbig		 " 360 361 362 363 364 365 366";
15231744Shelbig
15332139Shelbigint     flag_weeks;		/* user wants number of week */
15431744Shelbigint     nswitch;		/* user defined switch date */
15531744Shelbigint	nswitchb;		/* switch date for backward compatibility */
15632215Shelbigchar   *efmt;			/* strftime format string for printeaster() */
15731744Shelbig
15831744Shelbigchar   *center(char *s, char *t, int w);
15931744Shelbigvoid	mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
16031744Shelbigvoid    mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
16131744Shelbigvoid    mkweekdays(struct weekdays * wds);
16231744Shelbigvoid    printcc(void);
16331744Shelbigvoid    printeaster(int year, int julian, int orthodox);
16431744Shelbigvoid    printmonth(int year, int month, int jd_flag);
16531744Shelbigvoid    printmonthb(int year, int month, int jd_flag);
16631744Shelbigvoid    printyear(int year, int jd_flag);
16731744Shelbigvoid    printyearb(int year, int jd_flag);
16832282Shelbigint	firstday(int y, int m);
16931744Shelbigdate   *sdate(int ndays, struct date * d);
17031744Shelbigdate   *sdateb(int ndays, struct date * d);
17131744Shelbigint     sndays(struct date * d);
17231744Shelbigint     sndaysb(struct date * d);
17331744Shelbigvoid    usage(void);
17431744Shelbigint     weekdayb(int nd);
17531744Shelbig
17631744Shelbigint
17731744Shelbigmain(int argc, char *argv[])
17831744Shelbig{
17931744Shelbig	struct  djswitch *p, *q;	/* to search user defined switch date */
18031744Shelbig	date	never = {10000, 1, 1};	/* outside valid range of dates */
18131744Shelbig	date	ukswitch = {1752, 9, 2};/* switch date for Great Britain */
18231744Shelbig	int     ch;			/* holds the option character */
18331744Shelbig	int     m = 0;			/* month */
18431744Shelbig	int	y = 0;			/* year */
18531744Shelbig	int     flag_backward = 0;	/* user called cal--backward compat. */
18631744Shelbig	int     flag_hole_year = 0;	/* user wants the whole year */
18731744Shelbig	int	flag_julian_cal = 0;	/* user wants Julian Calendar */
18831744Shelbig	int     flag_julian_day = 0;	/* user wants the Julian day
18931744Shelbig					 * numbers */
19031744Shelbig	int	flag_orthodox = 0;	/* use wants Orthodox easter */
19131744Shelbig	int	flag_easter = 0;	/* use wants easter date */
19231744Shelbig	char	*cp;			/* character pointer */
19332215Shelbig	char    *locale;		/* locale to get country code */
19431744Shelbig
19532215Shelbig	/*
19632215Shelbig	 * Use locale to determine the country code,
19732215Shelbig	 * and use the country code to determine the default
19832215Shelbig	 * switchdate and date format from the switches table.
19932215Shelbig	 */
20032215Shelbig	if ((locale = setlocale(LC_TIME, "")) == NULL)
20132215Shelbig		warn("setlocale");
20232215Shelbig	if (locale == NULL || locale == "C")
20332215Shelbig		locale = "_US";
20432215Shelbig	q = switches + sizeof(switches) / sizeof(struct djswitch);
20532215Shelbig	for (p = switches; p != q; p++)
20632215Shelbig		if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
20732215Shelbig			break;
20832215Shelbig	if (p == q) {
20932215Shelbig		nswitch = ndaysj(&dftswitch->dt);
21032215Shelbig		efmt = dftswitch->efmt;
21132215Shelbig	} else {
21232215Shelbig		nswitch = ndaysj(&p->dt);
21332215Shelbig		efmt = p->efmt;
21432215Shelbig		dftswitch = p;
21532215Shelbig	}
21631744Shelbig
21732215Shelbig
21831744Shelbig	/*
21931744Shelbig	 * Get the filename portion of argv[0] and set flag_backward if
22031744Shelbig	 * this program is called "cal".
22131744Shelbig	 */
22231744Shelbig	for (cp = argv[0]; *cp; cp++)
22331744Shelbig		;
22431744Shelbig	while (cp >= argv[0] && *cp != '/')
22531744Shelbig		cp--;
22631744Shelbig	if (strcmp("cal", ++cp) == 0)
22731744Shelbig		flag_backward = 1;
22831744Shelbig
22931744Shelbig	/* Set the switch date to United Kingdom if backwards compatible */
23031744Shelbig	if (flag_backward)
23131744Shelbig		nswitchb = ndaysj(&ukswitch);
23231744Shelbig
23331744Shelbig	while ((ch = getopt(argc, argv, "Jejops:wy")) != -1)
23431744Shelbig		switch (ch) {
23531744Shelbig		case 'J':
23631744Shelbig			if (flag_backward)
23731744Shelbig				usage();
23831744Shelbig			nswitch = ndaysj(&never);
23931744Shelbig			flag_julian_cal = 1;
24031744Shelbig			break;
24131744Shelbig		case 'e':
24231744Shelbig			if (flag_backward)
24331744Shelbig				usage();
24431744Shelbig			flag_easter = 1;
24531744Shelbig			break;
24631744Shelbig		case 'j':
24731744Shelbig			flag_julian_day = 1;
24831744Shelbig			break;
24931744Shelbig		case 'o':
25031744Shelbig			if (flag_backward)
25131744Shelbig				usage();
25231744Shelbig			flag_orthodox = 1;
25331744Shelbig			flag_easter = 1;
25431744Shelbig			break;
25531744Shelbig		case 'p':
25631744Shelbig			if (flag_backward)
25731744Shelbig				usage();
25831744Shelbig			printcc();
25931744Shelbig			return (0);
26031744Shelbig			break;
26131744Shelbig		case 's':
26231744Shelbig			if (flag_backward)
26331744Shelbig				usage();
26431744Shelbig			q = switches +
26531744Shelbig			    sizeof(switches) / sizeof(struct djswitch);
26631744Shelbig			for (p = switches;
26731744Shelbig			     p != q && strcmp(p->cc, optarg) != 0; p++)
26831744Shelbig				;
26931744Shelbig			if (p == q)
27031744Shelbig				errx(EX_USAGE,
27131744Shelbig				    "%s: invalid country code", optarg);
27231744Shelbig			nswitch = ndaysj(&(p->dt));
27331744Shelbig			break;
27431744Shelbig		case 'w':
27531744Shelbig			if (flag_backward)
27631744Shelbig				usage();
27731744Shelbig			flag_weeks = 1;
27831744Shelbig			break;
27931744Shelbig		case 'y':
28031744Shelbig			flag_hole_year = 1;
28131744Shelbig			break;
28231744Shelbig		default:
28331744Shelbig			usage();
28431744Shelbig		}
28531744Shelbig
28631744Shelbig	argc -= optind;
28731744Shelbig	argv += optind;
28831744Shelbig
28931744Shelbig	if (argc == 0) {
29031744Shelbig		time_t t;
29131744Shelbig		struct tm *tm;
29231744Shelbig
29331744Shelbig		t = time(NULL);
29431744Shelbig		tm = localtime(&t);
29531744Shelbig		y = tm->tm_year + 1900;
29631744Shelbig		m = tm->tm_mon + 1;
29731744Shelbig	}
29831744Shelbig
29931744Shelbig	switch (argc) {
30031744Shelbig	case 2:
30131744Shelbig		if (flag_easter)
30231744Shelbig			usage();
30331744Shelbig		m = atoi(*argv++);
30431744Shelbig		if (m < 1 || m > 12)
30531744Shelbig			errx(EX_USAGE, "month %d not in range 1..12", m);
30631744Shelbig		/* FALLTHROUGH */
30731744Shelbig	case 1:
30831744Shelbig		y = atoi(*argv++);
30931744Shelbig		if (y < 1 || y > 9999)
31031744Shelbig			errx(EX_USAGE, "year %d not in range 1..9999", y);
31131744Shelbig		break;
31231744Shelbig	case 0:
31331744Shelbig		break;
31431744Shelbig	default:
31531744Shelbig		usage();
31631744Shelbig	}
31731744Shelbig
31831744Shelbig	if (flag_easter)
31931744Shelbig		printeaster(y, flag_julian_cal, flag_orthodox);
32031744Shelbig	else if (argc == 1 || flag_hole_year)
32131744Shelbig		if (flag_backward)
32231744Shelbig			printyearb(y, flag_julian_day);
32331744Shelbig		else
32431744Shelbig			printyear(y, flag_julian_day);
32531744Shelbig	else
32631744Shelbig		if (flag_backward)
32731744Shelbig			printmonthb(y, m, flag_julian_day);
32831744Shelbig		else
32931744Shelbig			printmonth(y, m, flag_julian_day);
33031744Shelbig
33131744Shelbig	return (0);
33231744Shelbig}
33331744Shelbig
33431744Shelbigvoid
33531744Shelbigusage(void)
33631744Shelbig{
33731744Shelbig
33831744Shelbig	fprintf(stderr,
33931744Shelbig	    "usage: cal [-jy] [month[year]]\n"
34031744Shelbig	    "usage: ncal [-Jjpwy] [-s country_code] [[month] year]\n"
34131744Shelbig	    "usage: ncal [-Jeo] [year]\n");
34231744Shelbig	exit(EX_USAGE);
34331744Shelbig}
34431744Shelbig
34531744Shelbig/* print the assumed switches for all countries */
34631744Shelbigvoid
34731744Shelbigprintcc(void)
34831744Shelbig{
34931744Shelbig	struct djswitch *p;
35031744Shelbig	int n;	/* number of lines to print */
35131744Shelbig	int m;	/* offset from left to right table entry on the same line */
35231744Shelbig
35332215Shelbig#define FSTR "%c%s %-15s%4d-%02d-%02d"
35432215Shelbig#define DFLT(p) ((p) == dftswitch ? '*' : ' ')
35532215Shelbig#define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
35631744Shelbig
35731744Shelbig	n = sizeof(switches) / sizeof(struct djswitch);
35831744Shelbig	m = (n + 1) / 2;
35931744Shelbig	n /= 2;
36032215Shelbig	for (p = switches; p != switches + n; p++)
36132215Shelbig		printf(FSTR"     "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
36231744Shelbig	if (m != n)
36332215Shelbig		printf(FSTR"\n", FSTRARG(p));
36431744Shelbig}
36531744Shelbig
36631744Shelbig/* print the date of easter sunday */
36731744Shelbigvoid
36831744Shelbigprinteaster(int y, int julian, int orthodox)
36931744Shelbig{
37032215Shelbig	date    dt;
37132215Shelbig	struct tm tm;
37232215Shelbig	char    buf[80];
37331744Shelbig
37431744Shelbig	/* force orthodox easter for years before 1583 */
37531744Shelbig	if (y < 1583)
37631744Shelbig		orthodox = 1;
37731744Shelbig
37831744Shelbig	if (orthodox)
37931744Shelbig		if (julian)
38031744Shelbig			easteroj(y, &dt);
38131744Shelbig		else
38231744Shelbig			easterog(y, &dt);
38331744Shelbig	else
38431744Shelbig		easterg(y, &dt);
38532215Shelbig
38632215Shelbig	memset(&tm, 0, sizeof(tm));
38732215Shelbig	tm.tm_year = dt.y - 1900;
38832215Shelbig	tm.tm_mon  = dt.m - 1;
38932215Shelbig	tm.tm_mday = dt.d;
39032215Shelbig	strftime(buf, sizeof(buf), efmt,  &tm);
39132215Shelbig	printf("%s\n", buf);
39231744Shelbig}
39331744Shelbig
39431744Shelbigvoid
39531744Shelbigprintmonth(int y, int m, int jd_flag)
39631744Shelbig{
39731744Shelbig	struct monthlines month;
39831744Shelbig	struct weekdays wds;
39931744Shelbig	int i;
40031744Shelbig
40131744Shelbig	mkmonth(y, m - 1, jd_flag, &month);
40231744Shelbig	mkweekdays(&wds);
40331744Shelbig	printf("    %s %d\n", month.name, y);
40431744Shelbig	for (i = 0; i != 7; i++)
40531744Shelbig		printf("%.2s%s\n", wds.names[i], month.lines[i]);
40631744Shelbig	if (flag_weeks)
40731744Shelbig		printf("  %s\n", month.weeks);
40831744Shelbig}
40931744Shelbig
41031744Shelbigvoid
41131744Shelbigprintmonthb(int y, int m, int jd_flag)
41231744Shelbig{
41331744Shelbig	struct monthlines month;
41431744Shelbig	struct weekdays wds;
41531744Shelbig	char s[MAX_WIDTH], t[MAX_WIDTH];
41631744Shelbig	int i;
41731744Shelbig	int mw;
41831744Shelbig
41931744Shelbig	mkmonthb(y, m - 1, jd_flag, &month);
42031744Shelbig	mkweekdays(&wds);
42131744Shelbig
42231744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
42331744Shelbig
42431744Shelbig	sprintf(s, "%s %d", month.name, y);
42531744Shelbig	printf("%s\n", center(t, s, mw));
42631744Shelbig
42731744Shelbig	if (jd_flag)
42831744Shelbig		printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
42931744Shelbig			wds.names[1], wds.names[2], wds.names[3],
43031744Shelbig			wds.names[4], wds.names[5]);
43131744Shelbig	else
43231744Shelbig		printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
43331744Shelbig			wds.names[1], wds.names[2], wds.names[3],
43431744Shelbig			wds.names[4], wds.names[5]);
43531744Shelbig
43631744Shelbig	for (i = 0; i != 6; i++)
43731744Shelbig		printf("%s\n", month.lines[i]+1);
43831744Shelbig}
43931744Shelbig
44031744Shelbigvoid
44131744Shelbigprintyear(int y, int jd_flag)
44231744Shelbig{
44331744Shelbig	struct monthlines year[12];
44431744Shelbig	struct weekdays wds;
44531744Shelbig	char    s[80], t[80];
44631744Shelbig	int     i, j;
44731744Shelbig	int     mpl;
44831744Shelbig	int     mw;
44931744Shelbig
45031744Shelbig	for (i = 0; i != 12; i++)
45131744Shelbig		mkmonth(y, i, jd_flag, year + i);
45231744Shelbig	mkweekdays(&wds);
45331744Shelbig	mpl = jd_flag ? 3 : 4;
45431744Shelbig	mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
45531744Shelbig
45631744Shelbig	sprintf(s, "%d", y);
45731744Shelbig	printf("%s\n", center(t, s, mpl * mw));
45831744Shelbig
45931744Shelbig	for (j = 0; j != 12; j += mpl) {
46031744Shelbig		printf("    %-*s%-*s",
46131744Shelbig		    mw, year[j].name,
46231744Shelbig		    mw, year[j + 1].name);
46331744Shelbig		if (mpl == 3)
46431744Shelbig			printf("%s\n", year[j + 2].name);
46531744Shelbig		else
46631744Shelbig			printf("%-*s%s\n",
46731744Shelbig		    	    mw, year[j + 2].name,
46831744Shelbig		    	    year[j + 3].name);
46931744Shelbig		for (i = 0; i != 7; i++) {
47031744Shelbig			printf("%.2s%-*s%-*s",
47131744Shelbig			    wds.names[i],
47231744Shelbig			    mw, year[j].lines[i],
47331744Shelbig			    mw, year[j + 1].lines[i]);
47431744Shelbig			if (mpl == 3)
47531744Shelbig				printf("%s\n", year[j + 2].lines[i]);
47631744Shelbig			else
47731744Shelbig				printf("%-*s%s\n",
47831744Shelbig			    	    mw, year[j + 2].lines[i],
47931744Shelbig			    	    year[j + 3].lines[i]);
48031744Shelbig		}
48131744Shelbig		if (flag_weeks)
48231744Shelbig			if (mpl == 3)
48331744Shelbig				printf("  %-*s%-*s%-s\n",
48431744Shelbig				    mw, year[j].weeks,
48531744Shelbig				    mw, year[j + 1].weeks,
48631744Shelbig				    year[j + 2].weeks);
48731744Shelbig			else
48831744Shelbig				printf("  %-*s%-*s%-*s%-s\n",
48931744Shelbig				    mw, year[j].weeks,
49031744Shelbig				    mw, year[j + 1].weeks,
49131744Shelbig				    mw, year[j + 2].weeks,
49231744Shelbig				    year[j + 3].weeks);
49331744Shelbig	}
49431744Shelbig}
49531744Shelbig
49631744Shelbigvoid
49731744Shelbigprintyearb(int y, int jd_flag)
49831744Shelbig{
49931744Shelbig	struct monthlines year[12];
50031744Shelbig	struct weekdays wds;
50131744Shelbig	char	s[80], t[80];
50231744Shelbig	int     i, j;
50331744Shelbig	int     mpl;
50431744Shelbig	int     mw;
50531744Shelbig
50631744Shelbig	for (i = 0; i != 12; i++)
50731744Shelbig		mkmonthb(y, i, jd_flag, year + i);
50831744Shelbig	mkweekdays(&wds);
50931744Shelbig	mpl = jd_flag ? 2 : 3;
51031744Shelbig	mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
51131744Shelbig
51231744Shelbig	sprintf(s, "%d", y);
51331744Shelbig	printf("%s\n\n", center(t, s, mw * mpl + mpl));
51431744Shelbig
51531744Shelbig	for (j = 0; j != 12; j += mpl) {
51631744Shelbig		printf("%-*s  ", mw, center(s, year[j].name, mw));
51731744Shelbig		if (mpl == 2)
51831744Shelbig			printf("%s\n", center(s, year[j + 1].name, mw));
51931744Shelbig		else
52031744Shelbig			printf("%-*s  %s\n", mw,
52131744Shelbig			    center(s, year[j + 1].name, mw),
52231744Shelbig			    center(t, year[j + 2].name, mw));
52331744Shelbig
52431744Shelbig		if (mpl == 2)
52531744Shelbig			printf(" %s %s %s %s %s %s %s "
52631744Shelbig			       " %s %s %s %s %s %s %.2s\n",
52731744Shelbig				wds.names[6], wds.names[0], wds.names[1],
52831744Shelbig				wds.names[2], wds.names[3], wds.names[4],
52931744Shelbig				wds.names[5],
53031744Shelbig				wds.names[6], wds.names[0], wds.names[1],
53131744Shelbig				wds.names[2], wds.names[3], wds.names[4],
53231744Shelbig				wds.names[5]);
53331744Shelbig		else
53431744Shelbig			printf("%s%s%s%s%s%s%s "
53531744Shelbig				"%s%s%s%s%s%s%s "
53631744Shelbig				"%s%s%s%s%s%s%.2s\n",
53731744Shelbig				wds.names[6], wds.names[0], wds.names[1],
53831744Shelbig				wds.names[2], wds.names[3], wds.names[4],
53931744Shelbig				wds.names[5],
54031744Shelbig				wds.names[6], wds.names[0], wds.names[1],
54131744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54231744Shelbig				wds.names[5],
54331744Shelbig				wds.names[6], wds.names[0], wds.names[1],
54431744Shelbig				wds.names[2], wds.names[3], wds.names[4],
54531744Shelbig				wds.names[5]);
54631744Shelbig		for (i = 0; i != 6; i++) {
54731744Shelbig			if (mpl == 2)
54831744Shelbig				printf("%-*s  %s\n",
54931744Shelbig			    mw, year[j].lines[i]+1,
55031744Shelbig			    year[j + 1].lines[i]+1);
55131744Shelbig			else
55231744Shelbig				printf("%-*s  %-*s  %s\n",
55331744Shelbig			    mw, year[j].lines[i]+1,
55431744Shelbig			    mw, year[j + 1].lines[i]+1,
55531744Shelbig			    year[j + 2].lines[i]+1);
55631744Shelbig
55731744Shelbig		}
55831744Shelbig	}
55931744Shelbig}
56031744Shelbig
56131744Shelbigvoid
56231744Shelbigmkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
56331744Shelbig{
56431744Shelbig
56531744Shelbig	struct tm tm;		/* for strftime printing local names of
56631744Shelbig				 * months */
56731744Shelbig	date    dt;		/* handy date */
56831744Shelbig	int     dw;		/* width of numbers */
56931744Shelbig	int     first;		/* first day of month */
57031744Shelbig	int     firstm;		/* first day of first week of month */
57131744Shelbig	int     i, j, k;	/* just indices */
57231744Shelbig	int     last;		/* the first day of next month */
57331744Shelbig	int     jan1 = 0;	/* the first day of this year */
57431744Shelbig	char   *ds;		/* pointer to day strings (daystr or
57531744Shelbig				 * jdaystr) */
57631744Shelbig
57731744Shelbig	/* Set name of month. */
57831744Shelbig	memset(&tm, 0, sizeof(tm));
57931744Shelbig	tm.tm_mon = m;
58031744Shelbig	strftime(mlines->name, sizeof(mlines->name), "%B", &tm);
58131744Shelbig
58231744Shelbig	/*
58331744Shelbig	 * Set first and last to the day number of the first day of this
58431744Shelbig	 * month and the first day of next month respectively. Set jan1 to
58532282Shelbig	 * the day number of the first day of this year.
58631744Shelbig	 */
58732282Shelbig	first = firstday(y, m + 1);
58832282Shelbig	if (m == 11)
58932282Shelbig		last = firstday(y + 1, 1);
59032282Shelbig	else
59132282Shelbig		last = firstday(y, m + 2);
59231744Shelbig
59332282Shelbig	if (jd_flag)
59432282Shelbig		jan1 = firstday(m, 1);
59531744Shelbig
59631744Shelbig	/*
59731744Shelbig	 * Set firstm to the day number of monday of the first week of
59831744Shelbig	 * this month. (This might be in the last month)
59931744Shelbig	 */
60031744Shelbig	firstm = first - weekday(first);
60131744Shelbig
60231744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
60331744Shelbig	if (jd_flag) {
60431744Shelbig		ds = jdaystr;
60531744Shelbig		dw = 4;
60631744Shelbig	} else {
60731744Shelbig		ds = daystr;
60831744Shelbig		dw = 3;
60931744Shelbig	}
61031744Shelbig
61131744Shelbig	/*
61231744Shelbig	 * Fill the lines with day of month or day of year (julian day)
61331744Shelbig	 * line index: i, each line is one weekday. column index: j, each
61431744Shelbig	 * column is one day number. print column index: k.
61531744Shelbig	 */
61631744Shelbig	for (i = 0; i != 7; i++) {
61731744Shelbig		for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
61831744Shelbig			if (j >= first) {
61931744Shelbig				if (jd_flag)
62031744Shelbig					dt.d = j - jan1 + 1;
62131744Shelbig				else
62231744Shelbig					sdate(j, &dt);
62331744Shelbig				memcpy(mlines->lines[i] + k,
62431744Shelbig				       ds + dt.d * dw, dw);
62531744Shelbig			} else
62631744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
62731744Shelbig		mlines->lines[i][k] = '\0';
62831744Shelbig
62931744Shelbig	}
63031744Shelbig
63131744Shelbig	/* fill the weeknumbers */
63231744Shelbig	if (flag_weeks) {
63331744Shelbig		for (j = firstm, k = 0; j < last;  k += dw, j += 7)
63431744Shelbig			if (j <= nswitch)
63531744Shelbig				memset(mlines->weeks + k, ' ', dw);
63631744Shelbig			else
63731744Shelbig				memcpy(mlines->weeks + k,
63831744Shelbig				    ds + week(j, &i)*dw, dw);
63931744Shelbig		mlines->weeks[k] = '\0';
64031744Shelbig	}
64131744Shelbig}
64231744Shelbig
64331744Shelbigvoid
64431744Shelbigmkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
64531744Shelbig{
64631744Shelbig
64731744Shelbig	struct tm tm;		/* for strftime printing local names of
64831744Shelbig				 * months */
64931744Shelbig	date    dt;		/* handy date */
65031744Shelbig	int     dw;		/* width of numbers */
65131744Shelbig	int     first;		/* first day of month */
65231744Shelbig	int     firsts;		/* sunday of first week of month */
65331744Shelbig	int     i, j, k;	/* just indices */
65431744Shelbig	int     jan1 = 0;	/* the first day of this year */
65531744Shelbig	int     last;		/* the first day of next month */
65631744Shelbig	char   *ds;		/* pointer to day strings (daystr or
65731744Shelbig				 * jdaystr) */
65831744Shelbig
65931744Shelbig	/* Set ds (daystring) and dw (daywidth) according to the jd_flag */
66031744Shelbig	if (jd_flag) {
66131744Shelbig		ds = jdaystr;
66231744Shelbig		dw = 4;
66331744Shelbig	} else {
66431744Shelbig		ds = daystr;
66531744Shelbig		dw = 3;
66631744Shelbig	}
66731744Shelbig
66831744Shelbig	/* Set name of month centered */
66931744Shelbig	memset(&tm, 0, sizeof(tm));
67031744Shelbig	tm.tm_mon = m;
67131744Shelbig	strftime(mlines->name, sizeof(mlines->name), "%B", &tm);
67231744Shelbig
67331744Shelbig	/*
67431744Shelbig	 * Set first and last to the day number of the first day of this
67531744Shelbig	 * month and the first day of next month respectively. Set jan1 to
67631744Shelbig	 * the day number of Jan 1st of this year.
67731744Shelbig	 */
67831744Shelbig	dt.y = y;
67931744Shelbig	dt.m = m + 1;
68031744Shelbig	dt.d = 1;
68131744Shelbig	first = sndaysb(&dt);
68231744Shelbig	if (m == 11) {
68331744Shelbig		dt.y = y + 1;
68431744Shelbig		dt.m = 1;
68531744Shelbig		dt.d = 1;
68631744Shelbig	} else {
68731744Shelbig		dt.y = y;
68831744Shelbig		dt.m = m + 2;
68931744Shelbig		dt.d = 1;
69031744Shelbig	}
69131744Shelbig	last = sndaysb(&dt);
69231744Shelbig
69331744Shelbig	if (jd_flag) {
69431744Shelbig		dt.y = y;
69531744Shelbig		dt.m = 1;
69631744Shelbig		dt.d = 1;
69731744Shelbig		jan1 = sndaysb(&dt);
69831744Shelbig	}
69931744Shelbig
70031744Shelbig	/*
70131744Shelbig	 * Set firsts to the day number of sunday of the first week of
70231744Shelbig	 * this month. (This might be in the last month)
70331744Shelbig	 */
70431744Shelbig	firsts = first - (weekday(first)+1) % 7;
70531744Shelbig
70631744Shelbig	/*
70731744Shelbig	 * Fill the lines with day of month or day of year (Julian day)
70831744Shelbig	 * line index: i, each line is one week. column index: j, each
70931744Shelbig	 * column is one day number. print column index: k.
71031744Shelbig	 */
71131744Shelbig	for (i = 0; i != 6; i++) {
71231744Shelbig		for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
71331744Shelbig		     j++, k += dw)
71431744Shelbig			if (j >= first) {
71531744Shelbig				if (jd_flag)
71631744Shelbig					dt.d = j - jan1 + 1;
71731744Shelbig				else
71831744Shelbig					sdateb(j, &dt);
71931744Shelbig				memcpy(mlines->lines[i] + k,
72031744Shelbig				       ds + dt.d * dw, dw);
72131744Shelbig			} else
72231744Shelbig				memcpy(mlines->lines[i] + k, "    ", dw);
72331744Shelbig		if (k == 0)
72431744Shelbig			mlines->lines[i][1] = '\0';
72531744Shelbig		else
72631744Shelbig			mlines->lines[i][k] = '\0';
72731744Shelbig	}
72831744Shelbig}
72931744Shelbig
73031744Shelbig/* Put the local names of weekdays into the wds */
73132139Shelbigvoid
73232139Shelbigmkweekdays(struct weekdays *wds)
73331744Shelbig{
73431744Shelbig	int i;
73531744Shelbig	struct tm tm;
73631744Shelbig
73731744Shelbig	memset(&tm, 0, sizeof(tm));
73831744Shelbig
73931744Shelbig	for (i = 0; i != 7; i++) {
74031744Shelbig		tm.tm_wday = (i+1) % 7;
74131744Shelbig		strftime(wds->names[i], 4, "%a", &tm);
74232139Shelbig		wds->names[i][2] = ' ';
74331744Shelbig	}
74431744Shelbig}
74531744Shelbig
74631744Shelbig/*
74732282Shelbig * Compute the day number of the first day in month.
74832282Shelbig * The day y-m-1 might be dropped due to Gregorian Reformation,
74932282Shelbig * so the answer is the smallest day number nd with sdate(nd) in
75032282Shelbig * the month m.
75132282Shelbig */
75232282Shelbigint
75332282Shelbigfirstday(int y, int m)
75432282Shelbig{
75532282Shelbig	date dt;
75632282Shelbig	int nd;
75732282Shelbig
75832282Shelbig	dt.y = y;
75932282Shelbig	dt.m = m;
76032282Shelbig	dt.d = 1;
76132282Shelbig	for (nd = sndays(&dt); sdate(nd, &dt)->m != m; nd++)
76232282Shelbig		;
76332282Shelbig	return (nd);
76432282Shelbig}
76532282Shelbig
76632282Shelbig/*
76731744Shelbig * Compute the number of days from date, obey the local switch from
76831744Shelbig * Julian to Gregorian if specified by the user.
76931744Shelbig */
77031744Shelbigint
77131744Shelbigsndays(struct date *d)
77231744Shelbig{
77331744Shelbig
77431744Shelbig	if (nswitch != 0)
77531744Shelbig		if (nswitch < ndaysj(d))
77631744Shelbig			return (ndaysg(d));
77731744Shelbig		else
77831744Shelbig			return (ndaysj(d));
77931744Shelbig	else
78031744Shelbig		return ndaysg(d);
78131744Shelbig}
78231744Shelbig
78331744Shelbig/*
78431744Shelbig * Compute the number of days from date, obey the switch from
78531744Shelbig * Julian to Gregorian as used by UK and her colonies.
78631744Shelbig */
78731744Shelbigint
78831744Shelbigsndaysb(struct date *d)
78931744Shelbig{
79031744Shelbig
79131744Shelbig	if (nswitchb < ndaysj(d))
79231744Shelbig		return (ndaysg(d));
79331744Shelbig	else
79431744Shelbig		return (ndaysj(d));
79531744Shelbig}
79631744Shelbig
79731744Shelbig/* Inverse of sndays */
79831744Shelbigstruct date *
79931744Shelbigsdate(int nd, struct date *d)
80031744Shelbig{
80131744Shelbig
80231744Shelbig	if (nswitch < nd)
80331744Shelbig		return (gdate(nd, d));
80431744Shelbig	else
80531744Shelbig		return (jdate(nd, d));
80631744Shelbig}
80731744Shelbig
80831744Shelbig/* Inverse of sndaysb */
80931744Shelbigstruct date *
81031744Shelbigsdateb(int nd, struct date *d)
81131744Shelbig{
81231744Shelbig
81331744Shelbig	if (nswitchb < nd)
81431744Shelbig		return (gdate(nd, d));
81531744Shelbig	else
81631744Shelbig		return (jdate(nd, d));
81731744Shelbig}
81831744Shelbig
81931744Shelbig/* Center string t in string s of length w by putting enough leading blanks */
82031744Shelbigchar *
82131744Shelbigcenter(char *s, char *t, int w)
82231744Shelbig{
82331744Shelbig	char blanks[80];
82431744Shelbig
82531744Shelbig	memset(blanks, ' ', sizeof(blanks));
82631744Shelbig	sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
82731744Shelbig	return (s);
82831744Shelbig}
829