2013-06-19 18:13:50 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2013-06-19 18:13:50 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#define MONTHMAX 100
|
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
static void drawcal(int, int, int, int, int, int);
|
|
|
|
static int dayofweek(int, int, int, int);
|
2014-11-13 20:24:47 +00:00
|
|
|
static int isleap(int);
|
2013-06-19 18:13:50 +00:00
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
static void
|
2013-06-19 20:10:26 +00:00
|
|
|
drawcal(int year, int month, int day, int ncols, int nmons, int fday)
|
2013-06-19 18:13:50 +00:00
|
|
|
{
|
|
|
|
char str[21];
|
|
|
|
int count[MONTHMAX];
|
2013-06-19 20:10:26 +00:00
|
|
|
int d, i, r, j;
|
2013-06-19 18:13:50 +00:00
|
|
|
int moff, yoff, cur, last, ndays, day1;
|
2013-06-19 20:10:26 +00:00
|
|
|
char *smon[] = {
|
2013-06-19 18:13:50 +00:00
|
|
|
" January", " February", " March",
|
|
|
|
" April", " May", " June",
|
|
|
|
" July", " August", " September",
|
|
|
|
" October", " November", " December" };
|
|
|
|
int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
int row = 0;
|
2013-06-19 20:10:26 +00:00
|
|
|
char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", };
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!ncols)
|
2014-04-01 13:26:10 +00:00
|
|
|
ncols = nmons;
|
2014-11-13 17:29:30 +00:00
|
|
|
while (nmons > 0) {
|
2013-06-19 18:13:50 +00:00
|
|
|
last = MIN(nmons, ncols);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < last; i++) {
|
2013-06-19 18:13:50 +00:00
|
|
|
moff = month + ncols * row + i - 1;
|
|
|
|
cur = moff % 12;
|
|
|
|
yoff = year + moff / 12;
|
|
|
|
|
2013-07-19 16:08:15 +00:00
|
|
|
snprintf(str, sizeof(str), "%s %d", smon[cur], yoff);
|
2013-06-19 18:13:50 +00:00
|
|
|
printf("%-20s ", str);
|
|
|
|
count[i] = 1;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (i = 0; i < last; i++) {
|
|
|
|
for (j = fday; j < LEN(days); j++)
|
2013-06-19 20:10:26 +00:00
|
|
|
printf("%s ", days[j]);
|
2014-11-13 17:29:30 +00:00
|
|
|
for (j = 0; j < fday; j++)
|
2013-06-19 20:10:26 +00:00
|
|
|
printf("%s ", days[j]);
|
|
|
|
printf(" ");
|
|
|
|
}
|
2013-06-19 18:13:50 +00:00
|
|
|
printf("\n");
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (r = 0; r < 6; r++) {
|
|
|
|
for (i = 0; i < last; i++) {
|
2013-06-19 18:13:50 +00:00
|
|
|
moff = month + ncols * row + i - 1;
|
|
|
|
cur = moff % 12;
|
|
|
|
yoff = year + moff / 12;
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
ndays = mdays[cur] + ((cur == 1) && isleap(yoff));
|
2013-06-19 20:10:26 +00:00
|
|
|
day1 = dayofweek(year, cur, 1, fday);
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (d = 0; d < 7; d++) {
|
|
|
|
if ((r || d >= day1) && count[i] <= ndays)
|
2013-06-19 18:13:50 +00:00
|
|
|
printf("%2d ", count[i]++);
|
|
|
|
else
|
|
|
|
printf(" ");
|
2013-06-19 20:10:26 +00:00
|
|
|
}
|
2013-06-19 18:13:50 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
nmons -= ncols;
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-06-19 20:10:26 +00:00
|
|
|
dayofweek(int year, int month, int day, int fday)
|
2013-06-19 18:13:50 +00:00
|
|
|
{
|
2014-07-04 20:23:52 +00:00
|
|
|
static int t[] = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2014-07-04 20:23:52 +00:00
|
|
|
day += 7 - fday;
|
|
|
|
year -= month < 2;
|
|
|
|
return (year + year / 4 - year / 100 + year / 400 + t[month] + day) % 7;
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 20:24:47 +00:00
|
|
|
static int
|
2013-06-19 18:13:50 +00:00
|
|
|
isleap(int year)
|
|
|
|
{
|
2014-11-13 17:29:30 +00:00
|
|
|
if (year % 400 == 0)
|
2014-11-13 20:24:47 +00:00
|
|
|
return 1;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (year % 100 == 0)
|
2014-11-13 20:24:47 +00:00
|
|
|
return 0;
|
2014-07-04 20:00:04 +00:00
|
|
|
return (year % 4 == 0);
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-06-19 21:24:34 +00:00
|
|
|
eprintf("usage: %s [-1] [-3] [-m] [-s] [-y] [-c columns]"
|
|
|
|
" [-f firstday] [-n nmonths] [ [ [day] month] year]\n",
|
2013-06-19 20:10:26 +00:00
|
|
|
argv0);
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2013-06-19 20:10:26 +00:00
|
|
|
int year, month, day, ncols, nmons, fday;
|
|
|
|
struct tm *ltime;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
ltime = localtime(&now);
|
|
|
|
year = ltime->tm_year + 1900;
|
|
|
|
month = ltime->tm_mon + 1;
|
|
|
|
day = ltime->tm_mday;
|
|
|
|
fday = 0;
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
ncols = 3;
|
|
|
|
nmons = 1;
|
2013-06-19 18:13:50 +00:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-06-19 20:10:26 +00:00
|
|
|
case '1':
|
|
|
|
nmons = 1;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
nmons = 3;
|
|
|
|
month -= 1;
|
|
|
|
if(month == 0) {
|
|
|
|
month = 12;
|
|
|
|
year--;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-19 18:13:50 +00:00
|
|
|
case 'c':
|
2013-06-19 20:10:26 +00:00
|
|
|
ncols = estrtol(EARGF(usage()), 0);
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
2013-06-19 20:10:26 +00:00
|
|
|
case 'f':
|
|
|
|
fday = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
case 'm': /* Monday */
|
|
|
|
fday = 1;
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2013-06-19 20:10:26 +00:00
|
|
|
nmons = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
case 's': /* Sunday */
|
|
|
|
fday = 0;
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
|
|
|
case 'y':
|
2013-06-19 20:10:26 +00:00
|
|
|
month = 1;
|
|
|
|
nmons = 12;
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (argc) {
|
2013-06-19 20:10:26 +00:00
|
|
|
case 3:
|
|
|
|
day = estrtol(argv[0], 0);
|
|
|
|
argv++;
|
|
|
|
case 2:
|
|
|
|
month = estrtol(argv[0], 0);
|
|
|
|
argv++;
|
|
|
|
case 1:
|
|
|
|
year = estrtol(argv[0], 0);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (ncols < 0 || month < 1 || month > 12 || nmons < 1 \
|
2013-06-19 20:57:03 +00:00
|
|
|
|| nmons > MONTHMAX || fday < 0 || fday > 6) {
|
2013-06-19 18:13:50 +00:00
|
|
|
usage();
|
2013-06-19 20:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
drawcal(year, month, day, ncols, nmons, fday);
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|