2013-06-19 18:13:50 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#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);
|
2013-06-19 18:13:50 +00:00
|
|
|
static bool isleap(int);
|
|
|
|
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
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
while(nmons > 0) {
|
2013-06-19 18:13:50 +00:00
|
|
|
last = MIN(nmons, ncols);
|
2013-06-19 20:10:26 +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;
|
|
|
|
|
|
|
|
sprintf(str, "%s %d", smon[cur], yoff);
|
|
|
|
printf("%-20s ", str);
|
|
|
|
count[i] = 1;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
for(i = 0; i < last; i++) {
|
|
|
|
for(j = fday; j < LEN(days); j++)
|
|
|
|
printf("%s ", days[j]);
|
|
|
|
for(j = 0; j < fday; j++)
|
|
|
|
printf("%s ", days[j]);
|
|
|
|
printf(" ");
|
|
|
|
}
|
2013-06-19 18:13:50 +00:00
|
|
|
printf("\n");
|
|
|
|
|
2013-06-19 20:10:26 +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;
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-06-19 20:10:26 +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
|
|
|
{
|
2013-06-19 20:10:26 +00:00
|
|
|
int a, y, m, d;
|
2013-06-19 18:13:50 +00:00
|
|
|
|
|
|
|
month++;
|
|
|
|
a = (14 - month) / 12;
|
|
|
|
y = year + 4800 - a;
|
2013-06-19 20:10:26 +00:00
|
|
|
m = month + 12 * a - 3;
|
|
|
|
d = (day + (153 * m + 2) / 5 + 365 * y + y / 4 - y / 100 + y \
|
|
|
|
/ 400 - 32045 + 1) % 7;
|
|
|
|
return (fday > d)? (7 - d) : (d - fday);
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
isleap(int year)
|
|
|
|
{
|
|
|
|
bool leap = false;
|
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
if(year % 4 == 0)
|
|
|
|
leap = true;
|
|
|
|
if(year % 100 == 0)
|
|
|
|
leap = false;
|
|
|
|
if(year % 400 == 0)
|
|
|
|
leap = true;
|
2013-06-19 18:13:50 +00:00
|
|
|
return leap;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-06-19 20:10:26 +00:00
|
|
|
eprintf("usage: %s [-c columns] [-m month] [-n number] [-y year]\n",
|
|
|
|
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;
|
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
switch(argc) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if(ncols < 0 || ncols > MONTHMAX || month < 1 || month > 12 \
|
|
|
|
|| nmons < 1 || 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
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|