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[] =
46 /* Width of one month with backward compatibility */
47 #define MONTH_WIDTH_B_J 27
48 #define MONTH_WIDTH_B 20
50 #define MONTH_WIDTH_J 24
51 #define MONTH_WIDTH 18
55 typedef struct date date;
58 wchar_t name[MAX_WIDTH + 1];
59 char lines[7][MAX_WIDTH + 1];
60 char weeks[MAX_WIDTH + 1];
67 /* The switches from Julian to Gregorian in some countries */
68 static struct djswitch {
69 const char *cc; /* Country code according to ISO 3166 */
70 const char *nm; /* Name of country */
71 date dt; /* Last day of Julian calendar */
73 {"AL", "Albania", {1912, 11, 30}},
74 {"AT", "Austria", {1583, 10, 5}},
75 {"AU", "Australia", {1752, 9, 2}},
76 {"BE", "Belgium", {1582, 12, 14}},
77 {"BG", "Bulgaria", {1916, 3, 18}},
78 {"CA", "Canada", {1752, 9, 2}},
79 {"CH", "Switzerland", {1655, 2, 28}},
80 {"CN", "China", {1911, 12, 18}},
81 {"CZ", "Czech Republic",{1584, 1, 6}},
82 {"DE", "Germany", {1700, 2, 18}},
83 {"DK", "Denmark", {1700, 2, 18}},
84 {"ES", "Spain", {1582, 10, 4}},
85 {"FI", "Finland", {1753, 2, 17}},
86 {"FR", "France", {1582, 12, 9}},
87 {"GB", "United Kingdom",{1752, 9, 2}},
88 {"GR", "Greece", {1924, 3, 9}},
89 {"HU", "Hungary", {1587, 10, 21}},
90 {"IS", "Iceland", {1700, 11, 16}},
91 {"IT", "Italy", {1582, 10, 4}},
92 {"JP", "Japan", {1918, 12, 18}},
93 {"LI", "Lithuania", {1918, 2, 1}},
94 {"LN", "Latin", {9999, 05, 31}},
95 {"LU", "Luxembourg", {1582, 12, 14}},
96 {"LV", "Latvia", {1918, 2, 1}},
97 {"NL", "Netherlands", {1582, 12, 14}},
98 {"NO", "Norway", {1700, 2, 18}},
99 {"PL", "Poland", {1582, 10, 4}},
100 {"PT", "Portugal", {1582, 10, 4}},
101 {"RO", "Romania", {1919, 3, 31}},
102 {"RU", "Russia", {1918, 1, 31}},
103 {"SI", "Slovenia", {1919, 3, 4}},
104 {"SW", "Sweden", {1753, 2, 17}},
105 {"TR", "Turkey", {1926, 12, 18}},
106 {"US", "United States", {1752, 9, 2}},
107 {"YU", "Yugoslavia", {1919, 3, 4}}
110 struct djswitch *dftswitch =
111 switches + sizeof(switches) / sizeof(struct djswitch) - 2;
112 /* default switch (should be "US") */
114 /* Table used to print day of month and week numbers */
115 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
116 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
117 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
118 " 48 49 50 51 52 53";
120 /* Table used to print day of year and week numbers */
121 char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
122 " 10 11 12 13 14 15 16 17 18 19"
123 " 20 21 22 23 24 25 26 27 28 29"
124 " 30 31 32 33 34 35 36 37 38 39"
125 " 40 41 42 43 44 45 46 47 48 49"
126 " 50 51 52 53 54 55 56 57 58 59"
127 " 60 61 62 63 64 65 66 67 68 69"
128 " 70 71 72 73 74 75 76 77 78 79"
129 " 80 81 82 83 84 85 86 87 88 89"
130 " 90 91 92 93 94 95 96 97 98 99"
131 " 100 101 102 103 104 105 106 107 108 109"
132 " 110 111 112 113 114 115 116 117 118 119"
133 " 120 121 122 123 124 125 126 127 128 129"
134 " 130 131 132 133 134 135 136 137 138 139"
135 " 140 141 142 143 144 145 146 147 148 149"
136 " 150 151 152 153 154 155 156 157 158 159"
137 " 160 161 162 163 164 165 166 167 168 169"
138 " 170 171 172 173 174 175 176 177 178 179"
139 " 180 181 182 183 184 185 186 187 188 189"
140 " 190 191 192 193 194 195 196 197 198 199"
141 " 200 201 202 203 204 205 206 207 208 209"
142 " 210 211 212 213 214 215 216 217 218 219"
143 " 220 221 222 223 224 225 226 227 228 229"
144 " 230 231 232 233 234 235 236 237 238 239"
145 " 240 241 242 243 244 245 246 247 248 249"
146 " 250 251 252 253 254 255 256 257 258 259"
147 " 260 261 262 263 264 265 266 267 268 269"
148 " 270 271 272 273 274 275 276 277 278 279"
149 " 280 281 282 283 284 285 286 287 288 289"
150 " 290 291 292 293 294 295 296 297 298 299"
151 " 300 301 302 303 304 305 306 307 308 309"
152 " 310 311 312 313 314 315 316 317 318 319"
153 " 320 321 322 323 324 325 326 327 328 329"
154 " 330 331 332 333 334 335 336 337 338 339"
155 " 340 341 342 343 344 345 346 347 348 349"
156 " 350 351 352 353 354 355 356 357 358 359"
157 " 360 361 362 363 364 365 366";
159 int flag_weeks; /* user wants number of week */
160 int nswitch; /* user defined switch date */
161 int nswitchb; /* switch date for backward compatibility */
163 char *center(char *s, char *t, int w);
164 wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
165 void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
166 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
167 void mkweekdays(struct weekdays * wds);
168 int parsemonth(const char *s);
170 void printeaster(int year, int julian, int orthodox);
171 void printmonth(int year, int month, int jd_flag);
172 void printmonthb(int year, int month, int jd_flag);
173 void printyear(int year, int jd_flag);
174 void printyearb(int year, int jd_flag);
175 int firstday(int y, int m);
176 date *sdate(int ndays, struct date * d);
177 date *sdateb(int ndays, struct date * d);
178 int sndays(struct date * d);
179 int sndaysb(struct date * d);
180 static void usage(void);
181 int weekdayb(int nd);
184 main(int argc, char *argv[])
186 struct djswitch *p, *q; /* to search user defined switch date */
187 date never = {10000, 1, 1}; /* outside valid range of dates */
188 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
189 int ch; /* holds the option character */
190 int m = 0; /* month */
191 int y = 0; /* year */
192 int flag_backward = 0; /* user called cal--backward compat. */
193 int flag_hole_year = 0; /* user wants the whole year */
194 int flag_julian_cal = 0; /* user wants Julian Calendar */
195 int flag_julian_day = 0; /* user wants the Julian day
197 int flag_orthodox = 0; /* use wants Orthodox easter */
198 int flag_easter = 0; /* use wants easter date */
199 char *cp; /* character pointer */
200 char *flag_month = NULL; /* requested month as string */
201 const char *locale; /* locale to get country code */
204 * Use locale to determine the country code,
205 * and use the country code to determine the default
206 * switchdate and date format from the switches table.
208 if (setlocale(LC_ALL, "") == NULL)
210 locale = setlocale(LC_TIME, NULL);
211 if (locale == NULL ||
212 strcmp(locale, "C") == 0 ||
213 strcmp(locale, "POSIX") == 0 ||
214 strcmp(locale, "ASCII") == 0 ||
215 strcmp(locale, "US-ASCII") == 0)
217 q = switches + sizeof(switches) / sizeof(struct djswitch);
218 for (p = switches; p != q; p++)
219 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
222 nswitch = ndaysj(&dftswitch->dt);
224 nswitch = ndaysj(&p->dt);
230 * Get the filename portion of argv[0] and set flag_backward if
231 * this program is called "cal".
233 cp = strrchr(argv[0], '/');
234 cp = (cp == NULL) ? argv[0] : cp + 1;
235 if (strcmp("cal", cp) == 0)
238 /* Set the switch date to United Kingdom if backwards compatible */
240 nswitchb = ndaysj(&ukswitch);
242 while ((ch = getopt(argc, argv, "Jejm:ops:wy")) != -1)
247 nswitch = ndaysj(&never);
277 sizeof(switches) / sizeof(struct djswitch);
279 p != q && strcmp(p->cc, optarg) != 0; p++)
283 "%s: invalid country code", optarg);
284 nswitch = ndaysj(&(p->dt));
305 flag_month = *argv++;
309 if (y < 1 || y > 9999)
310 errx(EX_USAGE, "year %d not in range 1..9999", y);
319 y = tm->tm_year + 1900;
327 if (flag_month != NULL) {
328 m = parsemonth(flag_month);
331 "%s is neither a month number (1..12) nor a name",
336 printeaster(y, flag_julian_cal, flag_orthodox);
337 else if (argc == 1 || flag_hole_year)
339 printyearb(y, flag_julian_day);
341 printyear(y, flag_julian_day);
344 printmonthb(y, m, flag_julian_day);
346 printmonth(y, m, flag_julian_day);
356 "usage: cal [-jy] [[month] year]\n"
357 " cal [-j] [-m month] [year]\n"
358 " ncal [-Jjpwy] [-s country_code] [[month] year]\n"
359 " ncal [-Jeo] [year]\n", stderr);
363 /* print the assumed switches for all countries */
368 int n; /* number of lines to print */
369 int m; /* offset from left to right table entry on the same line */
371 #define FSTR "%c%s %-15s%4d-%02d-%02d"
372 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
373 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
375 n = sizeof(switches) / sizeof(struct djswitch);
378 for (p = switches; p != switches + n; p++)
379 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
381 printf(FSTR"\n", FSTRARG(p));
384 /* print the date of easter sunday */
386 printeaster(int y, int julian, int orthodox)
391 static int d_first = -1;
394 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
395 /* force orthodox easter for years before 1583 */
407 memset(&tm, 0, sizeof(tm));
408 tm.tm_year = dt.y - 1900;
409 tm.tm_mon = dt.m - 1;
411 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
416 printmonth(int y, int m, int jd_flag)
418 struct monthlines month;
422 mkmonth(y, m - 1, jd_flag, &month);
424 printf(" %ls %d\n", month.name, y);
425 for (i = 0; i != 7; i++)
426 printf("%.2ls%s\n", wds.names[i], month.lines[i]);
428 printf(" %s\n", month.weeks);
432 printmonthb(int y, int m, int jd_flag)
434 struct monthlines month;
436 wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
440 mkmonthb(y, m - 1, jd_flag, &month);
443 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
445 swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
446 wprintf(L"%ls\n", wcenter(t, s, mw));
449 wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
450 wds.names[6], wds.names[0],
451 wds.names[1], wds.names[2], wds.names[3],
452 wds.names[4], wds.names[5]);
454 wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
455 wds.names[0], wds.names[1], wds.names[2], wds.names[3],
456 wds.names[4], wds.names[5]);
458 for (i = 0; i != 6; i++)
459 printf("%s\n", month.lines[i]+1);
463 printyear(int y, int jd_flag)
465 struct monthlines year[12];
472 for (i = 0; i != 12; i++)
473 mkmonth(y, i, jd_flag, year + i);
475 mpl = jd_flag ? 3 : 4;
476 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
479 printf("%s\n", center(t, s, mpl * mw));
481 for (j = 0; j != 12; j += mpl) {
482 printf(" %-*ls%-*ls",
484 mw, year[j + 1].name);
486 printf("%ls\n", year[j + 2].name);
489 mw, year[j + 2].name,
491 for (i = 0; i != 7; i++) {
492 printf("%.2ls%-*s%-*s",
494 mw, year[j].lines[i],
495 mw, year[j + 1].lines[i]);
497 printf("%s\n", year[j + 2].lines[i]);
500 mw, year[j + 2].lines[i],
501 year[j + 3].lines[i]);
505 printf(" %-*s%-*s%-s\n",
507 mw, year[j + 1].weeks,
510 printf(" %-*s%-*s%-*s%-s\n",
512 mw, year[j + 1].weeks,
513 mw, year[j + 2].weeks,
520 printyearb(int y, int jd_flag)
522 struct monthlines year[12];
525 wchar_t ws[80], wt[80];
530 for (i = 0; i != 12; i++)
531 mkmonthb(y, i, jd_flag, year + i);
533 mpl = jd_flag ? 2 : 3;
534 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
537 printf("%s\n\n", center(t, s, mw * mpl + mpl));
539 for (j = 0; j != 12; j += mpl) {
540 printf("%-*ls ", mw, wcenter(ws, year[j].name, mw));
542 printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
544 printf("%-*ls %ls\n", mw,
545 wcenter(ws, year[j + 1].name, mw),
546 wcenter(wt, year[j + 2].name, mw));
549 wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
550 " %ls %ls %ls %ls %ls %ls %.2ls\n",
551 wds.names[6], wds.names[0], wds.names[1],
552 wds.names[2], wds.names[3], wds.names[4],
554 wds.names[6], wds.names[0], wds.names[1],
555 wds.names[2], wds.names[3], wds.names[4],
558 wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
559 "%ls%ls%ls%ls%ls%ls%ls "
560 "%ls%ls%ls%ls%ls%ls%.2ls\n",
561 wds.names[6], wds.names[0], wds.names[1],
562 wds.names[2], wds.names[3], wds.names[4],
564 wds.names[6], wds.names[0], wds.names[1],
565 wds.names[2], wds.names[3], wds.names[4],
567 wds.names[6], wds.names[0], wds.names[1],
568 wds.names[2], wds.names[3], wds.names[4],
570 for (i = 0; i != 6; i++) {
573 mw, year[j].lines[i]+1,
574 year[j + 1].lines[i]+1);
576 printf("%-*s %-*s %s\n",
577 mw, year[j].lines[i]+1,
578 mw, year[j + 1].lines[i]+1,
579 year[j + 2].lines[i]+1);
586 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
589 struct tm tm; /* for strftime printing local names of
591 date dt; /* handy date */
592 int dw; /* width of numbers */
593 int first; /* first day of month */
594 int firstm; /* first day of first week of month */
595 int i, j, k; /* just indices */
596 int last; /* the first day of next month */
597 int jan1 = 0; /* the first day of this year */
598 char *ds; /* pointer to day strings (daystr or
601 /* Set name of month. */
602 memset(&tm, 0, sizeof(tm));
604 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
606 mlines->name[0] = towupper(mlines->name[0]);
609 * Set first and last to the day number of the first day of this
610 * month and the first day of next month respectively. Set jan1 to
611 * the day number of the first day of this year.
613 first = firstday(y, m + 1);
615 last = firstday(y + 1, 1);
617 last = firstday(y, m + 2);
620 jan1 = firstday(y, 1);
623 * Set firstm to the day number of monday of the first week of
624 * this month. (This might be in the last month)
626 firstm = first - weekday(first);
628 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
638 * Fill the lines with day of month or day of year (julian day)
639 * line index: i, each line is one weekday. column index: j, each
640 * column is one day number. print column index: k.
642 for (i = 0; i != 7; i++) {
643 for (j = firstm + i, k = 0; j < last; j += 7, k += dw)
649 memcpy(mlines->lines[i] + k,
652 memcpy(mlines->lines[i] + k, " ", dw);
653 mlines->lines[i][k] = '\0';
657 /* fill the weeknumbers */
659 for (j = firstm, k = 0; j < last; k += dw, j += 7)
661 memset(mlines->weeks + k, ' ', dw);
663 memcpy(mlines->weeks + k,
664 ds + week(j, &i)*dw, dw);
665 mlines->weeks[k] = '\0';
670 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
673 struct tm tm; /* for strftime printing local names of
675 date dt; /* handy date */
676 int dw; /* width of numbers */
677 int first; /* first day of month */
678 int firsts; /* sunday of first week of month */
679 int i, j, k; /* just indices */
680 int jan1 = 0; /* the first day of this year */
681 int last; /* the first day of next month */
682 char *ds; /* pointer to day strings (daystr or
685 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
694 /* Set name of month centered */
695 memset(&tm, 0, sizeof(tm));
697 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
699 mlines->name[0] = towupper(mlines->name[0]);
702 * Set first and last to the day number of the first day of this
703 * month and the first day of next month respectively. Set jan1 to
704 * the day number of Jan 1st of this year.
709 first = sndaysb(&dt);
729 * Set firsts to the day number of sunday of the first week of
730 * this month. (This might be in the last month)
732 firsts = first - (weekday(first)+1) % 7;
735 * Fill the lines with day of month or day of year (Julian day)
736 * line index: i, each line is one week. column index: j, each
737 * column is one day number. print column index: k.
739 for (i = 0; i != 6; i++) {
740 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
747 memcpy(mlines->lines[i] + k,
750 memcpy(mlines->lines[i] + k, " ", dw);
752 mlines->lines[i][1] = '\0';
754 mlines->lines[i][k] = '\0';
758 /* Put the local names of weekdays into the wds */
760 mkweekdays(struct weekdays *wds)
766 memset(&tm, 0, sizeof(tm));
768 for (i = 0; i != 7; i++) {
769 tm.tm_wday = (i+1) % 7;
770 wcsftime(buf, sizeof(buf), L"%a", &tm);
774 wcscpy(wds->names[i], L" ");
775 wcsncpy(wds->names[i] + 2 - len, buf, len);
780 * Compute the day number of the first
781 * existing date after the first day in month.
782 * (the first day in month and even the month might not exist!)
785 firstday(int y, int m)
796 if ((dt.m >= m && dt.y == y) || dt.y > y)
805 * Compute the number of days from date, obey the local switch from
806 * Julian to Gregorian if specified by the user.
809 sndays(struct date *d)
813 if (nswitch < ndaysj(d))
822 * Compute the number of days from date, obey the switch from
823 * Julian to Gregorian as used by UK and her colonies.
826 sndaysb(struct date *d)
829 if (nswitchb < ndaysj(d))
835 /* Inverse of sndays */
837 sdate(int nd, struct date *d)
841 return (gdate(nd, d));
843 return (jdate(nd, d));
846 /* Inverse of sndaysb */
848 sdateb(int nd, struct date *d)
852 return (gdate(nd, d));
854 return (jdate(nd, d));
857 /* Center string t in string s of length w by putting enough leading blanks */
859 center(char *s, char *t, int w)
863 memset(blanks, ' ', sizeof(blanks));
864 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
868 /* Center string t in string s of length w by putting enough leading blanks */
870 wcenter(wchar_t *s, wchar_t *t, int w)
874 memset(blanks, ' ', sizeof(blanks));
875 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
880 parsemonth(const char *s)
886 v = (int)strtol(s, &cp, 10);
889 if (strptime(s, "%B", &tm) != NULL)
890 return (tm.tm_mon + 1);
891 if (strptime(s, "%b", &tm) != NULL)
892 return (tm.tm_mon + 1);