2 * Copyright (c) 1997 Wolfgang Helbig
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 static const char rcsid[] =
29 "$Id: ncal.c,v 1.5 1998/01/07 07:46:33 charnier Exp $";
42 /* Width of one month with backward compatibility */
43 #define MONTH_WIDTH_B_J 27
44 #define MONTH_WIDTH_B 20
46 #define MONTH_WIDTH_J 24
47 #define MONTH_WIDTH 18
51 typedef struct date date;
54 char name[MAX_WIDTH + 1];
55 char lines[7][MAX_WIDTH + 1];
56 char weeks[MAX_WIDTH + 1];
63 /* The switches from Julian to Gregorian in some countries */
64 static struct djswitch {
65 char *cc; /* Country code according to ISO 3166 */
66 char *nm; /* Name of country */
67 date dt; /* Last day of Julian calendar */
68 char *efmt; /* strftime format for printing date of easter */
70 {"AL", "Albania", {1912, 11, 30}, "%e %B %Y"},
71 {"AT", "Austria", {1583, 10, 5}, "%e. %B %Y"},
72 {"AU", "Australia", {1752, 9, 2}, "%B %e, %Y"},
73 {"BE", "Belgium", {1582, 12, 14}, "%B %e, %Y"},
74 {"BG", "Bulgaria", {1916, 3, 18}, "%e %B %Y"},
75 {"CA", "Canada", {1752, 9, 2}, "%B %e, %Y"},
76 {"CH", "Switzerland", {1655, 2, 28}, "%e. %B %Y"},
77 {"CN", "China", {1911, 12, 18}, "%e %B %Y"},
78 {"CZ", "Czech Republic",{1584, 1, 6}, "%e %B %Y"},
79 {"DE", "Germany", {1700, 2, 18}, "%e. %B %Y"},
80 {"DK", "Denmark", {1700, 2, 18}, "%e. %B %Y"},
81 {"ES", "Spain", {1582, 10, 4}, "%e de %B de %Y"},
82 {"FI", "Finland", {1753, 2, 17}, "%e %B %Y"},
83 {"FR", "France", {1582, 12, 9}, "%e. %B %Y"},
84 {"GB", "United Kingdom",{1752, 9, 2}, "%e %B %Y"},
85 {"GR", "Greece", {1924, 3, 9}, "%e %B %Y"},
86 {"HU", "Hungary", {1587, 10, 21}, "%e %B %Y"},
87 {"IS", "Iceland", {1700, 11, 16}, "%e %B %Y"},
88 {"IT", "Italy", {1582, 10, 4}, "%e %B %Y"},
89 {"JP", "Japan", {1918, 12, 18}, "%Y\x94N %B%e"},
90 {"LI", "Lithuania", {1918, 2, 1}, "%e %B %Y"},
91 {"LN", "Latin", {9999, 31, 12}, "%e %B %Y"},
92 {"LU", "Luxembourg", {1582, 12, 14}, "%e %B %Y"},
93 {"LV", "Latvia", {1918, 2, 1}, "%e %B %Y"},
94 {"NL", "Netherlands", {1582, 12, 14}, "%e %B %Y"},
95 {"NO", "Norway", {1700, 2, 18}, "%e %B %Y"},
96 {"PL", "Poland", {1582, 10, 4}, "%e %B %Y"},
97 {"PT", "Portugal", {1582, 10, 4}, "%e %B %Y"},
98 {"RO", "Romania", {1919, 3, 31}, "%e %B %Y"},
99 {"RU", "Russia", {1918, 1, 31}, "%e %B %Y"},
100 {"SI", "Slovenia", {1919, 3, 4}, "%e %B %Y"},
101 {"SU", "USSR", {1920, 3, 4}, "%e %B %Y"},
102 {"SW", "Sweden", {1753, 2, 17}, "%e %B %Y"},
103 {"TR", "Turkey", {1926, 12, 18}, "%e %B %Y"},
104 {"US", "United States", {1752, 9, 2}, "%B %e, %Y"},
105 {"YU", "Yugoslavia", {1919, 3, 4}, "%e %B %Y"}
108 struct djswitch *dftswitch =
109 switches + sizeof(switches) / sizeof(struct djswitch) - 2;
110 /* default switch (should be "US") */
112 /* Table used to print day of month and week numbers */
113 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
114 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
115 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
116 " 48 49 50 51 52 53";
118 /* Table used to print day of year and week numbers */
119 char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
120 " 10 11 12 13 14 15 16 17 18 19"
121 " 20 21 22 23 24 25 26 27 28 29"
122 " 30 31 32 33 34 35 36 37 38 39"
123 " 40 41 42 43 44 45 46 47 48 49"
124 " 50 51 52 53 54 55 56 57 58 59"
125 " 60 61 62 63 64 65 66 67 68 69"
126 " 70 71 72 73 74 75 76 77 78 79"
127 " 80 81 82 83 84 85 86 87 88 89"
128 " 90 91 92 93 94 95 96 97 98 99"
129 " 100 101 102 103 104 105 106 107 108 109"
130 " 110 111 112 113 114 115 116 117 118 119"
131 " 120 121 122 123 124 125 126 127 128 129"
132 " 130 131 132 133 134 135 136 137 138 139"
133 " 140 141 142 143 144 145 146 147 148 149"
134 " 150 151 152 153 154 155 156 157 158 159"
135 " 160 161 162 163 164 165 166 167 168 169"
136 " 170 171 172 173 174 175 176 177 178 179"
137 " 180 181 182 183 184 185 186 187 188 189"
138 " 190 191 192 193 194 195 196 197 198 199"
139 " 200 201 202 203 204 205 206 207 208 209"
140 " 210 211 212 213 214 215 216 217 218 219"
141 " 220 221 222 223 224 225 226 227 228 229"
142 " 230 231 232 233 234 235 236 237 238 239"
143 " 240 241 242 243 244 245 246 247 248 249"
144 " 250 251 252 253 254 255 256 257 258 259"
145 " 260 261 262 263 264 265 266 267 268 269"
146 " 270 271 272 273 274 275 276 277 278 279"
147 " 280 281 282 283 284 285 286 287 288 289"
148 " 290 291 292 293 294 295 296 297 298 299"
149 " 300 301 302 303 304 305 306 307 308 309"
150 " 310 311 312 313 314 315 316 317 318 319"
151 " 320 321 322 323 324 325 326 327 328 329"
152 " 330 331 332 333 334 335 336 337 338 339"
153 " 340 341 342 343 344 345 346 347 348 349"
154 " 350 351 352 353 354 355 356 357 358 359"
155 " 360 361 362 363 364 365 366";
157 int flag_weeks; /* user wants number of week */
158 int nswitch; /* user defined switch date */
159 int nswitchb; /* switch date for backward compatibility */
160 char *efmt; /* strftime format string for printeaster() */
162 char *center(char *s, char *t, int w);
163 void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
164 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
165 void mkweekdays(struct weekdays * wds);
167 void printeaster(int year, int julian, int orthodox);
168 void printmonth(int year, int month, int jd_flag);
169 void printmonthb(int year, int month, int jd_flag);
170 void printyear(int year, int jd_flag);
171 void printyearb(int year, int jd_flag);
172 int firstday(int y, int m);
173 date *sdate(int ndays, struct date * d);
174 date *sdateb(int ndays, struct date * d);
175 int sndays(struct date * d);
176 int sndaysb(struct date * d);
177 static void usage(void);
178 int weekdayb(int nd);
181 main(int argc, char *argv[])
183 struct djswitch *p, *q; /* to search user defined switch date */
184 date never = {10000, 1, 1}; /* outside valid range of dates */
185 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
186 int ch; /* holds the option character */
187 int m = 0; /* month */
188 int y = 0; /* year */
189 int flag_backward = 0; /* user called cal--backward compat. */
190 int flag_hole_year = 0; /* user wants the whole year */
191 int flag_julian_cal = 0; /* user wants Julian Calendar */
192 int flag_julian_day = 0; /* user wants the Julian day
194 int flag_orthodox = 0; /* use wants Orthodox easter */
195 int flag_easter = 0; /* use wants easter date */
196 char *cp; /* character pointer */
197 char *locale; /* locale to get country code */
200 * Use locale to determine the country code,
201 * and use the country code to determine the default
202 * switchdate and date format from the switches table.
204 if ((locale = setlocale(LC_TIME, "")) == NULL)
206 if (locale == NULL || locale == "C")
208 q = switches + sizeof(switches) / sizeof(struct djswitch);
209 for (p = switches; p != q; p++)
210 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
213 nswitch = ndaysj(&dftswitch->dt);
214 efmt = dftswitch->efmt;
216 nswitch = ndaysj(&p->dt);
223 * Get the filename portion of argv[0] and set flag_backward if
224 * this program is called "cal".
226 for (cp = argv[0]; *cp; cp++)
228 while (cp >= argv[0] && *cp != '/')
230 if (strcmp("cal", ++cp) == 0)
233 /* Set the switch date to United Kingdom if backwards compatible */
235 nswitchb = ndaysj(&ukswitch);
237 while ((ch = getopt(argc, argv, "Jejops:wy")) != -1)
242 nswitch = ndaysj(&never);
269 sizeof(switches) / sizeof(struct djswitch);
271 p != q && strcmp(p->cc, optarg) != 0; p++)
275 "%s: invalid country code", optarg);
276 nswitch = ndaysj(&(p->dt));
299 y = tm->tm_year + 1900;
309 errx(EX_USAGE, "month %d not in range 1..12", m);
313 if (y < 1 || y > 9999)
314 errx(EX_USAGE, "year %d not in range 1..9999", y);
323 printeaster(y, flag_julian_cal, flag_orthodox);
324 else if (argc == 1 || flag_hole_year)
326 printyearb(y, flag_julian_day);
328 printyear(y, flag_julian_day);
331 printmonthb(y, m, flag_julian_day);
333 printmonth(y, m, flag_julian_day);
342 fprintf(stderr, "%s\n%s\n%s\n",
343 "usage: cal [-jy] [[month] year]",
344 " ncal [-Jjpwy] [-s country_code] [[month] year]",
345 " ncal [-Jeo] [year]");
349 /* print the assumed switches for all countries */
354 int n; /* number of lines to print */
355 int m; /* offset from left to right table entry on the same line */
357 #define FSTR "%c%s %-15s%4d-%02d-%02d"
358 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
359 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
361 n = sizeof(switches) / sizeof(struct djswitch);
364 for (p = switches; p != switches + n; p++)
365 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
367 printf(FSTR"\n", FSTRARG(p));
370 /* print the date of easter sunday */
372 printeaster(int y, int julian, int orthodox)
378 /* force orthodox easter for years before 1583 */
390 memset(&tm, 0, sizeof(tm));
391 tm.tm_year = dt.y - 1900;
392 tm.tm_mon = dt.m - 1;
394 strftime(buf, sizeof(buf), efmt, &tm);
399 printmonth(int y, int m, int jd_flag)
401 struct monthlines month;
405 mkmonth(y, m - 1, jd_flag, &month);
407 printf(" %s %d\n", month.name, y);
408 for (i = 0; i != 7; i++)
409 printf("%.2s%s\n", wds.names[i], month.lines[i]);
411 printf(" %s\n", month.weeks);
415 printmonthb(int y, int m, int jd_flag)
417 struct monthlines month;
419 char s[MAX_WIDTH], t[MAX_WIDTH];
423 mkmonthb(y, m - 1, jd_flag, &month);
426 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
428 sprintf(s, "%s %d", month.name, y);
429 printf("%s\n", center(t, s, mw));
432 printf(" %s %s %s %s %s %s %.2s\n", wds.names[6], wds.names[0],
433 wds.names[1], wds.names[2], wds.names[3],
434 wds.names[4], wds.names[5]);
436 printf("%s%s%s%s%s%s%.2s\n", wds.names[6], wds.names[0],
437 wds.names[1], wds.names[2], wds.names[3],
438 wds.names[4], wds.names[5]);
440 for (i = 0; i != 6; i++)
441 printf("%s\n", month.lines[i]+1);
445 printyear(int y, int jd_flag)
447 struct monthlines year[12];
454 for (i = 0; i != 12; i++)
455 mkmonth(y, i, jd_flag, year + i);
457 mpl = jd_flag ? 3 : 4;
458 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
461 printf("%s\n", center(t, s, mpl * mw));
463 for (j = 0; j != 12; j += mpl) {
466 mw, year[j + 1].name);
468 printf("%s\n", year[j + 2].name);
471 mw, year[j + 2].name,
473 for (i = 0; i != 7; i++) {
474 printf("%.2s%-*s%-*s",
476 mw, year[j].lines[i],
477 mw, year[j + 1].lines[i]);
479 printf("%s\n", year[j + 2].lines[i]);
482 mw, year[j + 2].lines[i],
483 year[j + 3].lines[i]);
487 printf(" %-*s%-*s%-s\n",
489 mw, year[j + 1].weeks,
492 printf(" %-*s%-*s%-*s%-s\n",
494 mw, year[j + 1].weeks,
495 mw, year[j + 2].weeks,
501 printyearb(int y, int jd_flag)
503 struct monthlines year[12];
510 for (i = 0; i != 12; i++)
511 mkmonthb(y, i, jd_flag, year + i);
513 mpl = jd_flag ? 2 : 3;
514 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
517 printf("%s\n\n", center(t, s, mw * mpl + mpl));
519 for (j = 0; j != 12; j += mpl) {
520 printf("%-*s ", mw, center(s, year[j].name, mw));
522 printf("%s\n", center(s, year[j + 1].name, mw));
524 printf("%-*s %s\n", mw,
525 center(s, year[j + 1].name, mw),
526 center(t, year[j + 2].name, mw));
529 printf(" %s %s %s %s %s %s %s "
530 " %s %s %s %s %s %s %.2s\n",
531 wds.names[6], wds.names[0], wds.names[1],
532 wds.names[2], wds.names[3], wds.names[4],
534 wds.names[6], wds.names[0], wds.names[1],
535 wds.names[2], wds.names[3], wds.names[4],
538 printf("%s%s%s%s%s%s%s "
540 "%s%s%s%s%s%s%.2s\n",
541 wds.names[6], wds.names[0], wds.names[1],
542 wds.names[2], wds.names[3], wds.names[4],
544 wds.names[6], wds.names[0], wds.names[1],
545 wds.names[2], wds.names[3], wds.names[4],
547 wds.names[6], wds.names[0], wds.names[1],
548 wds.names[2], wds.names[3], wds.names[4],
550 for (i = 0; i != 6; i++) {
553 mw, year[j].lines[i]+1,
554 year[j + 1].lines[i]+1);
556 printf("%-*s %-*s %s\n",
557 mw, year[j].lines[i]+1,
558 mw, year[j + 1].lines[i]+1,
559 year[j + 2].lines[i]+1);
566 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
569 struct tm tm; /* for strftime printing local names of
571 date dt; /* handy date */
572 int dw; /* width of numbers */
573 int first; /* first day of month */
574 int firstm; /* first day of first week of month */
575 int i, j, k; /* just indices */
576 int last; /* the first day of next month */
577 int jan1 = 0; /* the first day of this year */
578 char *ds; /* pointer to day strings (daystr or
581 /* Set name of month. */
582 memset(&tm, 0, sizeof(tm));
584 strftime(mlines->name, sizeof(mlines->name), "%B", &tm);
587 * Set first and last to the day number of the first day of this
588 * month and the first day of next month respectively. Set jan1 to
589 * the day number of the first day of this year.
591 first = firstday(y, m + 1);
593 last = firstday(y + 1, 1);
595 last = firstday(y, m + 2);
598 jan1 = firstday(y, 1);
601 * Set firstm to the day number of monday of the first week of
602 * this month. (This might be in the last month)
604 firstm = first - weekday(first);
606 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
616 * Fill the lines with day of month or day of year (julian day)
617 * line index: i, each line is one weekday. column index: j, each
618 * column is one day number. print column index: k.
620 for (i = 0; i != 7; i++) {
621 for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
627 memcpy(mlines->lines[i] + k,
630 memcpy(mlines->lines[i] + k, " ", dw);
631 mlines->lines[i][k] = '\0';
635 /* fill the weeknumbers */
637 for (j = firstm, k = 0; j < last; k += dw, j += 7)
639 memset(mlines->weeks + k, ' ', dw);
641 memcpy(mlines->weeks + k,
642 ds + week(j, &i)*dw, dw);
643 mlines->weeks[k] = '\0';
648 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
651 struct tm tm; /* for strftime printing local names of
653 date dt; /* handy date */
654 int dw; /* width of numbers */
655 int first; /* first day of month */
656 int firsts; /* sunday of first week of month */
657 int i, j, k; /* just indices */
658 int jan1 = 0; /* the first day of this year */
659 int last; /* the first day of next month */
660 char *ds; /* pointer to day strings (daystr or
663 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
672 /* Set name of month centered */
673 memset(&tm, 0, sizeof(tm));
675 strftime(mlines->name, sizeof(mlines->name), "%B", &tm);
678 * Set first and last to the day number of the first day of this
679 * month and the first day of next month respectively. Set jan1 to
680 * the day number of Jan 1st of this year.
685 first = sndaysb(&dt);
705 * Set firsts to the day number of sunday of the first week of
706 * this month. (This might be in the last month)
708 firsts = first - (weekday(first)+1) % 7;
711 * Fill the lines with day of month or day of year (Julian day)
712 * line index: i, each line is one week. column index: j, each
713 * column is one day number. print column index: k.
715 for (i = 0; i != 6; i++) {
716 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
723 memcpy(mlines->lines[i] + k,
726 memcpy(mlines->lines[i] + k, " ", dw);
728 mlines->lines[i][1] = '\0';
730 mlines->lines[i][k] = '\0';
734 /* Put the local names of weekdays into the wds */
736 mkweekdays(struct weekdays *wds)
741 memset(&tm, 0, sizeof(tm));
743 for (i = 0; i != 7; i++) {
744 tm.tm_wday = (i+1) % 7;
745 strftime(wds->names[i], 4, "%a", &tm);
746 wds->names[i][2] = ' ';
751 * Compute the day number of the first day in month.
752 * The day y-m-1 might be dropped due to Gregorian Reformation,
753 * so the answer is the smallest day number nd with sdate(nd) in
757 firstday(int y, int m)
765 for (nd = sndays(&dt); sdate(nd, &dt)->m != m; nd++)
771 * Compute the number of days from date, obey the local switch from
772 * Julian to Gregorian if specified by the user.
775 sndays(struct date *d)
779 if (nswitch < ndaysj(d))
788 * Compute the number of days from date, obey the switch from
789 * Julian to Gregorian as used by UK and her colonies.
792 sndaysb(struct date *d)
795 if (nswitchb < ndaysj(d))
801 /* Inverse of sndays */
803 sdate(int nd, struct date *d)
807 return (gdate(nd, d));
809 return (jdate(nd, d));
812 /* Inverse of sndaysb */
814 sdateb(int nd, struct date *d)
818 return (gdate(nd, d));
820 return (jdate(nd, d));
823 /* Center string t in string s of length w by putting enough leading blanks */
825 center(char *s, char *t, int w)
829 memset(blanks, ' ', sizeof(blanks));
830 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);