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