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 #undef lines /* term.h defines this */
48 /* Width of one month with backward compatibility */
49 #define MONTH_WIDTH_B_J 27
50 #define MONTH_WIDTH_B 20
52 #define MONTH_WIDTH_J 24
53 #define MONTH_WIDTH 18
57 typedef struct date date;
60 wchar_t name[MAX_WIDTH + 1];
61 char lines[7][MAX_WIDTH + 1];
62 char weeks[MAX_WIDTH + 1];
69 /* The switches from Julian to Gregorian in some countries */
70 static struct djswitch {
71 const char *cc; /* Country code according to ISO 3166 */
72 const char *nm; /* Name of country */
73 date dt; /* Last day of Julian calendar */
75 {"AL", "Albania", {1912, 11, 30}},
76 {"AT", "Austria", {1583, 10, 5}},
77 {"AU", "Australia", {1752, 9, 2}},
78 {"BE", "Belgium", {1582, 12, 14}},
79 {"BG", "Bulgaria", {1916, 3, 18}},
80 {"CA", "Canada", {1752, 9, 2}},
81 {"CH", "Switzerland", {1655, 2, 28}},
82 {"CN", "China", {1911, 12, 18}},
83 {"CZ", "Czech Republic",{1584, 1, 6}},
84 {"DE", "Germany", {1700, 2, 18}},
85 {"DK", "Denmark", {1700, 2, 18}},
86 {"ES", "Spain", {1582, 10, 4}},
87 {"FI", "Finland", {1753, 2, 17}},
88 {"FR", "France", {1582, 12, 9}},
89 {"GB", "United Kingdom",{1752, 9, 2}},
90 {"GR", "Greece", {1924, 3, 9}},
91 {"HU", "Hungary", {1587, 10, 21}},
92 {"IS", "Iceland", {1700, 11, 16}},
93 {"IT", "Italy", {1582, 10, 4}},
94 {"JP", "Japan", {1918, 12, 18}},
95 {"LI", "Lithuania", {1918, 2, 1}},
96 {"LN", "Latin", {9999, 05, 31}},
97 {"LU", "Luxembourg", {1582, 12, 14}},
98 {"LV", "Latvia", {1918, 2, 1}},
99 {"NL", "Netherlands", {1582, 12, 14}},
100 {"NO", "Norway", {1700, 2, 18}},
101 {"PL", "Poland", {1582, 10, 4}},
102 {"PT", "Portugal", {1582, 10, 4}},
103 {"RO", "Romania", {1919, 3, 31}},
104 {"RU", "Russia", {1918, 1, 31}},
105 {"SI", "Slovenia", {1919, 3, 4}},
106 {"SW", "Sweden", {1753, 2, 17}},
107 {"TR", "Turkey", {1926, 12, 18}},
108 {"US", "United States", {1752, 9, 2}},
109 {"YU", "Yugoslavia", {1919, 3, 4}}
112 struct djswitch *dftswitch =
113 switches + sizeof(switches) / sizeof(struct djswitch) - 2;
114 /* default switch (should be "US") */
116 /* Table used to print day of month and week numbers */
117 char daystr[] = " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15"
118 " 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31"
119 " 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47"
120 " 48 49 50 51 52 53";
122 /* Table used to print day of year and week numbers */
123 char jdaystr[] = " 1 2 3 4 5 6 7 8 9"
124 " 10 11 12 13 14 15 16 17 18 19"
125 " 20 21 22 23 24 25 26 27 28 29"
126 " 30 31 32 33 34 35 36 37 38 39"
127 " 40 41 42 43 44 45 46 47 48 49"
128 " 50 51 52 53 54 55 56 57 58 59"
129 " 60 61 62 63 64 65 66 67 68 69"
130 " 70 71 72 73 74 75 76 77 78 79"
131 " 80 81 82 83 84 85 86 87 88 89"
132 " 90 91 92 93 94 95 96 97 98 99"
133 " 100 101 102 103 104 105 106 107 108 109"
134 " 110 111 112 113 114 115 116 117 118 119"
135 " 120 121 122 123 124 125 126 127 128 129"
136 " 130 131 132 133 134 135 136 137 138 139"
137 " 140 141 142 143 144 145 146 147 148 149"
138 " 150 151 152 153 154 155 156 157 158 159"
139 " 160 161 162 163 164 165 166 167 168 169"
140 " 170 171 172 173 174 175 176 177 178 179"
141 " 180 181 182 183 184 185 186 187 188 189"
142 " 190 191 192 193 194 195 196 197 198 199"
143 " 200 201 202 203 204 205 206 207 208 209"
144 " 210 211 212 213 214 215 216 217 218 219"
145 " 220 221 222 223 224 225 226 227 228 229"
146 " 230 231 232 233 234 235 236 237 238 239"
147 " 240 241 242 243 244 245 246 247 248 249"
148 " 250 251 252 253 254 255 256 257 258 259"
149 " 260 261 262 263 264 265 266 267 268 269"
150 " 270 271 272 273 274 275 276 277 278 279"
151 " 280 281 282 283 284 285 286 287 288 289"
152 " 290 291 292 293 294 295 296 297 298 299"
153 " 300 301 302 303 304 305 306 307 308 309"
154 " 310 311 312 313 314 315 316 317 318 319"
155 " 320 321 322 323 324 325 326 327 328 329"
156 " 330 331 332 333 334 335 336 337 338 339"
157 " 340 341 342 343 344 345 346 347 348 349"
158 " 350 351 352 353 354 355 356 357 358 359"
159 " 360 361 362 363 364 365 366";
161 int flag_weeks; /* user wants number of week */
162 int nswitch; /* user defined switch date */
163 int nswitchb; /* switch date for backward compatibility */
164 const char *term_so, *term_se;
167 char *center(char *s, char *t, int w);
168 wchar_t *wcenter(wchar_t *s, wchar_t *t, int w);
169 void mkmonth(int year, int month, int jd_flag, struct monthlines * monthl);
170 void mkmonthb(int year, int month, int jd_flag, struct monthlines * monthl);
171 void mkweekdays(struct weekdays * wds);
172 int parsemonth(const char *s, int *m, int *y);
174 void printeaster(int year, int julian, int orthodox);
175 void printmonth(int year, int month, int jd_flag);
176 void printmonthb(int year, int month, int jd_flag);
177 void printyear(int year, int jd_flag);
178 void printyearb(int year, int jd_flag);
179 int firstday(int y, int m);
180 date *sdate(int ndays, struct date * d);
181 date *sdateb(int ndays, struct date * d);
182 int sndays(struct date * d);
183 int sndaysb(struct date * d);
184 static void usage(void);
185 int weekdayb(int nd);
188 main(int argc, char *argv[])
190 struct djswitch *p, *q; /* to search user defined switch date */
191 date never = {10000, 1, 1}; /* outside valid range of dates */
192 date ukswitch = {1752, 9, 2};/* switch date for Great Britain */
193 int ch; /* holds the option character */
194 int m = 0; /* month */
195 int y = 0; /* year */
196 int flag_backward = 0; /* user called cal--backward compat. */
197 int flag_hole_year = 0; /* user wants the whole year */
198 int flag_julian_cal = 0; /* user wants Julian Calendar */
199 int flag_julian_day = 0; /* user wants the Julian day
201 int flag_orthodox = 0; /* use wants Orthodox easter */
202 int flag_easter = 0; /* use wants easter date */
203 char *cp; /* character pointer */
204 char *flag_month = NULL; /* requested month as string */
205 const char *locale; /* locale to get country code */
206 char tbuf[1024], cbuf[512], *b;
210 term_se = term_so = NULL;
212 if (isatty(STDOUT_FILENO) && tgetent(tbuf, NULL) == 1) {
213 date dt; /* handy date */
216 term_so = tgetstr("so", &b);
217 term_se = tgetstr("se", &b);
220 dt.y = tm1->tm_year + 1900;
221 dt.m = tm1->tm_mon + 1;
224 today = sndaysb(&dt);
228 * Use locale to determine the country code,
229 * and use the country code to determine the default
230 * switchdate and date format from the switches table.
232 if (setlocale(LC_ALL, "") == NULL)
234 locale = setlocale(LC_TIME, NULL);
235 if (locale == NULL ||
236 strcmp(locale, "C") == 0 ||
237 strcmp(locale, "POSIX") == 0 ||
238 strcmp(locale, "ASCII") == 0 ||
239 strcmp(locale, "US-ASCII") == 0)
241 q = switches + sizeof(switches) / sizeof(struct djswitch);
242 for (p = switches; p != q; p++)
243 if ((cp = strstr(locale, p->cc)) != NULL && *(cp - 1) == '_')
246 nswitch = ndaysj(&dftswitch->dt);
248 nswitch = ndaysj(&p->dt);
254 * Get the filename portion of argv[0] and set flag_backward if
255 * this program is called "cal".
257 cp = strrchr(argv[0], '/');
258 cp = (cp == NULL) ? argv[0] : cp + 1;
259 if (strcmp("cal", cp) == 0)
262 /* Set the switch date to United Kingdom if backwards compatible */
264 nswitchb = ndaysj(&ukswitch);
266 while ((ch = getopt(argc, argv, "Jehjm:ops:wy")) != -1)
271 nswitch = ndaysj(&never);
275 term_so = term_se = NULL;
304 sizeof(switches) / sizeof(struct djswitch);
306 p != q && strcmp(p->cc, optarg) != 0; p++)
310 "%s: invalid country code", optarg);
311 nswitch = ndaysj(&(p->dt));
332 flag_month = *argv++;
336 if (y < 1 || y > 9999)
337 errx(EX_USAGE, "year %d not in range 1..9999", y);
346 y = tm->tm_year + 1900;
354 if (flag_month != NULL) {
355 if (parsemonth(flag_month, &m, &y)) {
357 "%s is neither a month number (1..12) nor a name",
363 printeaster(y, flag_julian_cal, flag_orthodox);
364 else if (argc == 1 || flag_hole_year) {
365 /* disable the highlight for now */
368 printyearb(y, flag_julian_day);
370 printyear(y, flag_julian_day);
373 printmonthb(y, m, flag_julian_day);
375 printmonth(y, m, flag_julian_day);
385 "usage: cal [-hjy] [[month] year]\n"
386 " cal [-hj] [-m month] [year]\n"
387 " ncal [-hJjpwy] [-s country_code] [[month] year]\n"
388 " ncal [-hJeo] [year]\n", stderr);
392 /* print the assumed switches for all countries */
397 int n; /* number of lines to print */
398 int m; /* offset from left to right table entry on the same line */
400 #define FSTR "%c%s %-15s%4d-%02d-%02d"
401 #define DFLT(p) ((p) == dftswitch ? '*' : ' ')
402 #define FSTRARG(p) DFLT(p), (p)->cc, (p)->nm, (p)->dt.y, (p)->dt.m, (p)->dt.d
404 n = sizeof(switches) / sizeof(struct djswitch);
407 for (p = switches; p != switches + n; p++)
408 printf(FSTR" "FSTR"\n", FSTRARG(p), FSTRARG(p+m));
410 printf(FSTR"\n", FSTRARG(p));
413 /* print the date of easter sunday */
415 printeaster(int y, int julian, int orthodox)
420 static int d_first = -1;
423 d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
424 /* force orthodox easter for years before 1583 */
436 memset(&tm, 0, sizeof(tm));
437 tm.tm_year = dt.y - 1900;
438 tm.tm_mon = dt.m - 1;
440 strftime(buf, sizeof(buf), d_first ? "%e %B %Y" : "%B %e %Y", &tm);
445 printmonth(int y, int m, int jd_flag)
447 struct monthlines month;
451 mkmonth(y, m - 1, jd_flag, &month);
453 printf(" %ls %d\n", month.name, y);
454 for (i = 0; i != 7; i++)
455 wprintf(L"%.2ls%s\n", wds.names[i], month.lines[i]);
457 printf(" %s\n", month.weeks);
461 printmonthb(int y, int m, int jd_flag)
463 struct monthlines month;
465 wchar_t s[MAX_WIDTH], t[MAX_WIDTH];
469 mkmonthb(y, m - 1, jd_flag, &month);
472 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
474 swprintf(s, MAX_WIDTH, L"%ls %d", month.name, y);
475 wprintf(L"%ls\n", wcenter(t, s, mw));
478 wprintf(L" %ls %ls %ls %ls %ls %ls %.2ls\n",
479 wds.names[6], wds.names[0],
480 wds.names[1], wds.names[2], wds.names[3],
481 wds.names[4], wds.names[5]);
483 wprintf(L"%ls%ls%ls%ls%ls%ls%.2ls\n", wds.names[6],
484 wds.names[0], wds.names[1], wds.names[2], wds.names[3],
485 wds.names[4], wds.names[5]);
487 for (i = 0; i != 6; i++)
488 printf("%s\n", month.lines[i]+1);
492 printyear(int y, int jd_flag)
494 struct monthlines year[12];
501 for (i = 0; i != 12; i++)
502 mkmonth(y, i, jd_flag, year + i);
504 mpl = jd_flag ? 3 : 4;
505 mw = jd_flag ? MONTH_WIDTH_J : MONTH_WIDTH;
508 printf("%s\n", center(t, s, mpl * mw));
510 for (j = 0; j != 12; j += mpl) {
511 wprintf(L" %-*ls%-*ls",
513 mw, year[j + 1].name);
515 printf("%ls\n", year[j + 2].name);
517 wprintf(L"%-*ls%ls\n",
518 mw, year[j + 2].name,
520 for (i = 0; i != 7; i++) {
521 wprintf(L"%.2ls%-*s%-*s",
523 mw, year[j].lines[i],
524 mw, year[j + 1].lines[i]);
526 printf("%s\n", year[j + 2].lines[i]);
529 mw, year[j + 2].lines[i],
530 year[j + 3].lines[i]);
534 printf(" %-*s%-*s%-s\n",
536 mw, year[j + 1].weeks,
539 printf(" %-*s%-*s%-*s%-s\n",
541 mw, year[j + 1].weeks,
542 mw, year[j + 2].weeks,
549 printyearb(int y, int jd_flag)
551 struct monthlines year[12];
554 wchar_t ws[80], wt[80];
559 for (i = 0; i != 12; i++)
560 mkmonthb(y, i, jd_flag, year + i);
562 mpl = jd_flag ? 2 : 3;
563 mw = jd_flag ? MONTH_WIDTH_B_J : MONTH_WIDTH_B;
566 printf("%s\n\n", center(t, s, mw * mpl + mpl));
568 for (j = 0; j != 12; j += mpl) {
569 wprintf(L"%-*ls ", mw, wcenter(ws, year[j].name, mw));
571 printf("%ls\n", wcenter(ws, year[j + 1].name, mw));
573 wprintf(L"%-*ls %ls\n", mw,
574 wcenter(ws, year[j + 1].name, mw),
575 wcenter(wt, year[j + 2].name, mw));
578 wprintf(L" %ls %ls %ls %ls %ls %ls %ls "
579 " %ls %ls %ls %ls %ls %ls %.2ls\n",
580 wds.names[6], wds.names[0], wds.names[1],
581 wds.names[2], wds.names[3], wds.names[4],
583 wds.names[6], wds.names[0], wds.names[1],
584 wds.names[2], wds.names[3], wds.names[4],
587 wprintf(L"%ls%ls%ls%ls%ls%ls%ls "
588 "%ls%ls%ls%ls%ls%ls%ls "
589 "%ls%ls%ls%ls%ls%ls%.2ls\n",
590 wds.names[6], wds.names[0], wds.names[1],
591 wds.names[2], wds.names[3], wds.names[4],
593 wds.names[6], wds.names[0], wds.names[1],
594 wds.names[2], wds.names[3], wds.names[4],
596 wds.names[6], wds.names[0], wds.names[1],
597 wds.names[2], wds.names[3], wds.names[4],
599 for (i = 0; i != 6; i++) {
602 mw, year[j].lines[i]+1,
603 year[j + 1].lines[i]+1);
605 printf("%-*s %-*s %s\n",
606 mw, year[j].lines[i]+1,
607 mw, year[j + 1].lines[i]+1,
608 year[j + 2].lines[i]+1);
615 mkmonth(int y, int m, int jd_flag, struct monthlines *mlines)
618 struct tm tm; /* for strftime printing local names of
620 date dt; /* handy date */
621 int dw; /* width of numbers */
622 int first; /* first day of month */
623 int firstm; /* first day of first week of month */
624 int i, j, k, l; /* just indices */
625 int last; /* the first day of next month */
626 int jan1 = 0; /* the first day of this year */
627 char *ds; /* pointer to day strings (daystr or
630 /* Set name of month. */
631 memset(&tm, 0, sizeof(tm));
633 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
635 mlines->name[0] = towupper(mlines->name[0]);
638 * Set first and last to the day number of the first day of this
639 * month and the first day of next month respectively. Set jan1 to
640 * the day number of the first day of this year.
642 first = firstday(y, m + 1);
644 last = firstday(y + 1, 1);
646 last = firstday(y, m + 2);
649 jan1 = firstday(y, 1);
652 * Set firstm to the day number of monday of the first week of
653 * this month. (This might be in the last month)
655 firstm = first - weekday(first);
657 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
667 * Fill the lines with day of month or day of year (julian day)
668 * line index: i, each line is one weekday. column index: j, each
669 * column is one day number. print column index: k.
671 for (i = 0; i != 7; i++) {
673 for (j = firstm + i, k = 0; j < last; j += 7, k += dw) {
674 if (j == today && (term_so != NULL && term_se != NULL)) {
681 mlines->lines[i][k] = ' ';
682 /* the actual text */
683 memcpy(mlines->lines[i] + k + l,
686 memcpy(mlines->lines[i] + k + 1, term_so, l);
688 memcpy(mlines->lines[i] + k + l + dw, term_se,
690 l = strlen(term_se) + strlen(term_so);
698 memcpy(mlines->lines[i] + k + l,
701 memcpy(mlines->lines[i] + k + l, " ", dw);
703 mlines->lines[i][k + l] = '\0';
707 /* fill the weeknumbers */
709 for (j = firstm, k = 0; j < last; k += dw, j += 7)
711 memset(mlines->weeks + k, ' ', dw);
713 memcpy(mlines->weeks + k,
714 ds + week(j, &i)*dw, dw);
715 mlines->weeks[k] = '\0';
720 mkmonthb(int y, int m, int jd_flag, struct monthlines *mlines)
723 struct tm tm; /* for strftime printing local names of
725 date dt; /* handy date */
726 int dw; /* width of numbers */
727 int first; /* first day of month */
728 int firsts; /* sunday of first week of month */
729 int i, j, k, l; /* just indices */
730 int jan1 = 0; /* the first day of this year */
731 int last; /* the first day of next month */
732 char *ds; /* pointer to day strings (daystr or
735 /* Set ds (daystring) and dw (daywidth) according to the jd_flag */
744 /* Set name of month centered */
745 memset(&tm, 0, sizeof(tm));
747 wcsftime(mlines->name, sizeof(mlines->name) / sizeof(mlines->name[0]),
749 mlines->name[0] = towupper(mlines->name[0]);
752 * Set first and last to the day number of the first day of this
753 * month and the first day of next month respectively. Set jan1 to
754 * the day number of Jan 1st of this year.
759 first = sndaysb(&dt);
779 * Set firsts to the day number of sunday of the first week of
780 * this month. (This might be in the last month)
782 firsts = first - (weekday(first)+1) % 7;
785 * Fill the lines with day of month or day of year (Julian day)
786 * line index: i, each line is one week. column index: j, each
787 * column is one day number. print column index: k.
789 for (i = 0; i != 6; i++) {
791 for (j = firsts + 7 * i, k = 0; j < last && k != dw * 7;
793 if (j == today && (term_so != NULL && term_se != NULL)) {
800 mlines->lines[i][k] = ' ';
801 /* the actual text */
802 memcpy(mlines->lines[i] + k + l,
805 memcpy(mlines->lines[i] + k + 1, term_so, l);
807 memcpy(mlines->lines[i] + k + l + dw, term_se,
809 l = strlen(term_se) + strlen(term_so);
817 memcpy(mlines->lines[i] + k + l,
820 memcpy(mlines->lines[i] + k + l, " ", dw);
823 mlines->lines[i][1] = '\0';
825 mlines->lines[i][k + l] = '\0';
829 /* Put the local names of weekdays into the wds */
831 mkweekdays(struct weekdays *wds)
837 memset(&tm, 0, sizeof(tm));
839 for (i = 0; i != 7; i++) {
840 tm.tm_wday = (i+1) % 7;
841 wcsftime(buf, sizeof(buf), L"%a", &tm);
845 wcscpy(wds->names[i], L" ");
846 wcsncpy(wds->names[i] + 2 - len, buf, len);
851 * Compute the day number of the first
852 * existing date after the first day in month.
853 * (the first day in month and even the month might not exist!)
856 firstday(int y, int m)
867 if ((dt.m >= m && dt.y == y) || dt.y > y)
876 * Compute the number of days from date, obey the local switch from
877 * Julian to Gregorian if specified by the user.
880 sndays(struct date *d)
884 if (nswitch < ndaysj(d))
893 * Compute the number of days from date, obey the switch from
894 * Julian to Gregorian as used by UK and her colonies.
897 sndaysb(struct date *d)
900 if (nswitchb < ndaysj(d))
906 /* Inverse of sndays */
908 sdate(int nd, struct date *d)
912 return (gdate(nd, d));
914 return (jdate(nd, d));
917 /* Inverse of sndaysb */
919 sdateb(int nd, struct date *d)
923 return (gdate(nd, d));
925 return (jdate(nd, d));
928 /* Center string t in string s of length w by putting enough leading blanks */
930 center(char *s, char *t, int w)
934 memset(blanks, ' ', sizeof(blanks));
935 sprintf(s, "%.*s%s", (int)(w - strlen(t)) / 2, blanks, t);
939 /* Center string t in string s of length w by putting enough leading blanks */
941 wcenter(wchar_t *s, wchar_t *t, int w)
945 memset(blanks, ' ', sizeof(blanks));
946 swprintf(s, MAX_WIDTH, L"%.*s%ls", (int)(w - wcslen(t)) / 2, blanks, t);
951 parsemonth(const char *s, int *m, int *y)
957 nm = (int)strtol(s, &cp, 10);
961 ; /* no special action */
962 } else if (*cp == 'f' || *cp == 'F') {
965 } else if (*cp == 'p' || *cp == 'P') {
970 if (nm < 1 || nm > 12)
976 if (strptime(s, "%B", &tm) != NULL || strptime(s, "%b", &tm) != NULL) {