2013-06-19 18:13:50 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-30 15:01:10 +00:00
|
|
|
#include <limits.h>
|
2015-03-08 20:28:32 +00:00
|
|
|
#include <stdint.h>
|
2013-06-19 18:13:50 +00:00
|
|
|
#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"
|
|
|
|
|
2015-01-16 21:17:54 +00:00
|
|
|
enum { JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC };
|
|
|
|
enum caltype { JULIAN, GREGORIAN };
|
|
|
|
enum { TRANS_YEAR = 1752, TRANS_MONTH = SEP, TRANS_DAY = 2 };
|
2015-01-08 22:16:19 +00:00
|
|
|
|
2015-01-02 13:12:59 +00:00
|
|
|
static int
|
2015-03-08 20:28:32 +00:00
|
|
|
isleap(size_t year, enum caltype cal)
|
2015-01-02 13:12:59 +00:00
|
|
|
{
|
2015-01-16 21:17:54 +00:00
|
|
|
if (cal == GREGORIAN) {
|
2015-01-08 22:16:19 +00:00
|
|
|
if (year % 400 == 0)
|
|
|
|
return 1;
|
|
|
|
if (year % 100 == 0)
|
|
|
|
return 0;
|
|
|
|
return (year % 4 == 0);
|
|
|
|
}
|
|
|
|
else { /* cal == Julian */
|
|
|
|
return (year % 4 == 0);
|
|
|
|
}
|
2015-01-02 13:12:59 +00:00
|
|
|
}
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2015-01-02 13:12:59 +00:00
|
|
|
static int
|
2015-03-08 20:28:32 +00:00
|
|
|
monthlength(size_t year, int month, enum caltype cal)
|
2015-01-02 13:12:59 +00:00
|
|
|
{
|
|
|
|
int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
2015-01-16 21:17:54 +00:00
|
|
|
|
2015-03-08 20:28:32 +00:00
|
|
|
return (month == FEB && isleap(year, cal)) ? 29 : mdays[month];
|
2015-01-02 13:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* From http://www.tondering.dk/claus/cal/chrweek.php#calcdow */
|
|
|
|
static int
|
2015-03-08 20:28:32 +00:00
|
|
|
dayofweek(size_t year, int month, int dom, enum caltype cal)
|
2015-01-02 13:12:59 +00:00
|
|
|
{
|
2015-03-08 20:28:32 +00:00
|
|
|
size_t y;
|
|
|
|
int m, a;
|
2015-01-16 21:17:54 +00:00
|
|
|
|
2015-03-08 20:28:32 +00:00
|
|
|
a = (13 - month) / 12;
|
2015-01-02 13:12:59 +00:00
|
|
|
y = year - a;
|
2015-03-08 20:28:32 +00:00
|
|
|
m = month + 12 * a - 1;
|
2015-01-08 22:16:19 +00:00
|
|
|
|
2015-01-16 21:17:54 +00:00
|
|
|
if (cal == GREGORIAN)
|
|
|
|
return (dom + y + y / 4 - y / 100 + y / 400 + (31 * m) / 12) % 7;
|
2015-01-08 22:16:19 +00:00
|
|
|
else /* cal == Julian */
|
2015-01-16 21:17:54 +00:00
|
|
|
return (5 + dom + y + y / 4 + (31 * m) / 12) % 7;
|
2015-01-02 13:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-08 20:28:32 +00:00
|
|
|
printgrid(size_t year, int month, int fday, int line)
|
2015-01-02 13:12:59 +00:00
|
|
|
{
|
2015-01-08 22:16:19 +00:00
|
|
|
enum caltype cal;
|
2015-03-08 20:28:32 +00:00
|
|
|
int offset, dom, d = 0, trans; /* are we in the transition from Julian to Gregorian? */
|
2015-01-02 13:12:59 +00:00
|
|
|
|
2015-03-08 20:28:32 +00:00
|
|
|
cal = (year < TRANS_YEAR || (year == TRANS_YEAR && month <= TRANS_MONTH)) ? JULIAN : GREGORIAN;
|
2015-01-08 22:16:19 +00:00
|
|
|
trans = (year == TRANS_YEAR && month == TRANS_MONTH);
|
|
|
|
offset = dayofweek(year, month, 1, cal) - fday;
|
2015-03-08 20:28:32 +00:00
|
|
|
|
2015-01-02 13:12:59 +00:00
|
|
|
if (offset < 0)
|
|
|
|
offset += 7;
|
2015-01-16 21:17:54 +00:00
|
|
|
if (line == 1) {
|
2015-03-08 20:28:32 +00:00
|
|
|
for (; d < offset; ++d)
|
2015-01-02 13:12:59 +00:00
|
|
|
printf(" ");
|
|
|
|
dom = 1;
|
|
|
|
} else {
|
2015-01-16 21:17:54 +00:00
|
|
|
dom = 8 - offset + (line - 2) * 7;
|
|
|
|
if (trans && !(line == 2 && fday == 3))
|
2015-01-08 22:16:19 +00:00
|
|
|
dom += 11;
|
2015-01-02 13:12:59 +00:00
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
for (; d < 7 && dom <= monthlength(year, month, cal); ++d, ++dom) {
|
2015-01-02 13:12:59 +00:00
|
|
|
printf("%2d ", dom);
|
2015-03-08 20:28:32 +00:00
|
|
|
if (trans && dom == TRANS_DAY)
|
2015-01-08 22:16:19 +00:00
|
|
|
dom += 11;
|
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
for (; d < 7; ++d)
|
2015-01-02 13:12:59 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2013-06-19 18:13:50 +00:00
|
|
|
|
|
|
|
static void
|
2015-03-08 20:28:32 +00:00
|
|
|
drawcal(size_t year, int month, size_t ncols, size_t nmons, int fday)
|
2013-06-19 18:13:50 +00:00
|
|
|
{
|
2015-01-02 13:12:59 +00:00
|
|
|
char *smon[] = {" January", " February", " March", " April",
|
|
|
|
" May", " June", " July", " August",
|
|
|
|
"September", " October", " November", " December" };
|
2013-06-19 20:10:26 +00:00
|
|
|
char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", };
|
2015-03-08 20:28:32 +00:00
|
|
|
size_t m, n, col, cur_year, cur_month, dow;
|
|
|
|
int line;
|
2015-01-02 13:12:59 +00:00
|
|
|
|
|
|
|
for (m = 0; m < nmons; ) {
|
|
|
|
n = m;
|
|
|
|
for (col = 0; m < nmons && col < ncols; ++col, ++m) {
|
2015-01-16 21:17:54 +00:00
|
|
|
cur_year = year + m / 12;
|
|
|
|
cur_month = month + m % 12;
|
2015-01-02 13:12:59 +00:00
|
|
|
if (cur_month > 11) {
|
|
|
|
cur_month -= 12;
|
|
|
|
cur_year += 1;
|
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
printf(" %s %zu ", smon[cur_month], cur_year);
|
2015-01-02 13:12:59 +00:00
|
|
|
printf(" ");
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
putchar('\n');
|
2015-01-02 13:12:59 +00:00
|
|
|
for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) {
|
2015-01-16 21:17:54 +00:00
|
|
|
for (dow = fday; dow < (fday + 7); ++dow)
|
|
|
|
printf("%s ", days[dow % 7]);
|
2013-06-19 20:10:26 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
putchar('\n');
|
2015-01-16 21:17:54 +00:00
|
|
|
for (line = 1; line <= 6; ++line) {
|
|
|
|
for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) {
|
|
|
|
cur_year = year + m / 12;
|
|
|
|
cur_month = month + m % 12;
|
2015-01-02 13:12:59 +00:00
|
|
|
if (cur_month > 11) {
|
|
|
|
cur_month -= 12;
|
|
|
|
cur_year += 1;
|
2013-06-19 20:10:26 +00:00
|
|
|
}
|
2015-01-02 13:12:59 +00:00
|
|
|
printgrid(cur_year, cur_month, fday, line);
|
2013-06-19 18:13:50 +00:00
|
|
|
printf(" ");
|
|
|
|
}
|
2015-03-08 20:28:32 +00:00
|
|
|
putchar('\n');
|
2013-06-19 18:13:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-08 20:28:32 +00:00
|
|
|
eprintf("usage: %s [-1 | -3 | -y | -n num] "
|
|
|
|
"[-s | -m | -f num] [-c num] [[month] 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
|
|
|
struct tm *ltime;
|
|
|
|
time_t now;
|
2015-03-08 20:28:32 +00:00
|
|
|
size_t year, ncols, nmons;
|
|
|
|
int fday, month;
|
2013-06-19 20:10:26 +00:00
|
|
|
|
2015-01-30 15:01:10 +00:00
|
|
|
now = time(NULL);
|
2013-06-19 20:10:26 +00:00
|
|
|
ltime = localtime(&now);
|
2015-01-30 15:01:10 +00:00
|
|
|
year = ltime->tm_year + 1900;
|
2013-06-19 20:10:26 +00:00
|
|
|
month = ltime->tm_mon + 1;
|
2015-01-30 15:01:10 +00:00
|
|
|
fday = 0;
|
2013-06-19 18:13:50 +00:00
|
|
|
|
2013-06-19 20:10:26 +00:00
|
|
|
ncols = 3;
|
2015-01-08 19:21:37 +00:00
|
|
|
nmons = 0;
|
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;
|
2015-03-08 20:28:32 +00:00
|
|
|
if (--month == 0) {
|
2013-06-19 20:10:26 +00:00
|
|
|
month = 12;
|
|
|
|
year--;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-19 18:13:50 +00:00
|
|
|
case 'c':
|
2015-03-08 20:28:32 +00:00
|
|
|
ncols = estrtonum(EARGF(usage()), 0, MIN(SIZE_MAX, LLONG_MAX));
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
2013-06-19 20:10:26 +00:00
|
|
|
case 'f':
|
2015-01-30 15:01:10 +00:00
|
|
|
fday = estrtonum(EARGF(usage()), 0, 6);
|
2013-06-19 20:10:26 +00:00
|
|
|
break;
|
|
|
|
case 'm': /* Monday */
|
|
|
|
fday = 1;
|
2013-06-19 18:13:50 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
2015-03-08 20:35:16 +00:00
|
|
|
nmons = estrtonum(EARGF(usage()), 1, MIN(SIZE_MAX, LLONG_MAX));
|
2013-06-19 20:10:26 +00:00
|
|
|
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;
|
|
|
|
|
2015-01-08 19:21:37 +00:00
|
|
|
if (nmons == 0) {
|
|
|
|
if (argc == 1) {
|
|
|
|
month = 1;
|
|
|
|
nmons = 12;
|
|
|
|
} else {
|
|
|
|
nmons = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
switch (argc) {
|
2013-06-19 20:10:26 +00:00
|
|
|
case 2:
|
2015-01-30 15:01:10 +00:00
|
|
|
month = estrtonum(argv[0], 1, 12);
|
2013-06-19 20:10:26 +00:00
|
|
|
argv++;
|
|
|
|
case 1:
|
2015-01-30 15:01:10 +00:00
|
|
|
year = estrtonum(argv[0], 0, INT_MAX);
|
2013-06-19 20:10:26 +00:00
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2015-01-16 21:17:54 +00:00
|
|
|
drawcal(year, month - 1, 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
|
|
|
}
|