2013-06-19 14:13:50 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2013-06-19 14:13:50 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-01-02 08:12:59 -05:00
|
|
|
static int
|
|
|
|
isleap(int year)
|
|
|
|
{
|
|
|
|
if (year % 400 == 0)
|
|
|
|
return 1;
|
|
|
|
if (year % 100 == 0)
|
|
|
|
return 0;
|
|
|
|
return (year % 4 == 0);
|
|
|
|
}
|
2013-06-19 14:13:50 -04:00
|
|
|
|
2015-01-02 08:12:59 -05:00
|
|
|
static int
|
|
|
|
monthlength(int year, int month)
|
|
|
|
{
|
|
|
|
int mdays[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
|
|
|
|
return (month==1 && isleap(year)) ? 29 : mdays[month];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* From http://www.tondering.dk/claus/cal/chrweek.php#calcdow */
|
|
|
|
static int
|
|
|
|
dayofweek(int year, int month, int dom)
|
|
|
|
{
|
|
|
|
int m, y, a;
|
|
|
|
month += 1; /* in this formula, 1 <= month <= 12 */
|
|
|
|
a = (14 - month) / 12;
|
|
|
|
y = year - a;
|
|
|
|
m = month + 12*a - 2;
|
|
|
|
return (dom + y + y/4 - y/100 + y/400 +((31*m)/12)) % 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
printgrid(int year, int month, int fday, int line)
|
|
|
|
{
|
|
|
|
int dom, offset, d=0;
|
|
|
|
|
|
|
|
offset = dayofweek(year, month, 1) - fday;
|
|
|
|
if (offset < 0)
|
|
|
|
offset += 7;
|
|
|
|
if (line==1) {
|
|
|
|
for ( ; d < offset; ++d)
|
|
|
|
printf(" ");
|
|
|
|
dom = 1;
|
|
|
|
} else {
|
|
|
|
dom = 8-offset + (line-2)*7;
|
|
|
|
}
|
|
|
|
for ( ; d < 7 && dom <= monthlength(year, month); ++d, ++dom)
|
|
|
|
printf("%2d ", dom);
|
|
|
|
for ( ; d < 7; ++d)
|
|
|
|
printf(" ");
|
|
|
|
}
|
2013-06-19 14:13:50 -04:00
|
|
|
|
|
|
|
static void
|
2015-01-06 15:14:56 -05:00
|
|
|
drawcal(int year, int month, int ncols, int nmons, int fday)
|
2013-06-19 14:13:50 -04:00
|
|
|
{
|
2015-01-02 08:12:59 -05:00
|
|
|
char *smon[] = {" January", " February", " March", " April",
|
|
|
|
" May", " June", " July", " August",
|
|
|
|
"September", " October", " November", " December" };
|
2013-06-19 16:10:26 -04:00
|
|
|
char *days[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa", };
|
2015-01-02 08:12:59 -05:00
|
|
|
int m, n, col, cur_year, cur_month, line, dow;
|
|
|
|
|
|
|
|
for (m = 0; m < nmons; ) {
|
|
|
|
n = m;
|
|
|
|
for (col = 0; m < nmons && col < ncols; ++col, ++m) {
|
|
|
|
cur_year = year + m/12;
|
|
|
|
cur_month = month + m%12;
|
|
|
|
if (cur_month > 11) {
|
|
|
|
cur_month -= 12;
|
|
|
|
cur_year += 1;
|
|
|
|
}
|
|
|
|
printf(" %s %d ", smon[cur_month], cur_year);
|
|
|
|
printf(" ");
|
2013-06-19 14:13:50 -04:00
|
|
|
}
|
|
|
|
printf("\n");
|
2015-01-02 08:12:59 -05:00
|
|
|
for (col = 0, m = n; m < nmons && col < ncols; ++col, ++m) {
|
|
|
|
for (dow = fday; dow < (fday+7); ++dow)
|
|
|
|
printf("%s ", days[dow%7]);
|
2013-06-19 16:10:26 -04:00
|
|
|
printf(" ");
|
|
|
|
}
|
2013-06-19 14:13:50 -04:00
|
|
|
printf("\n");
|
2015-01-02 08:12:59 -05: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;
|
|
|
|
if (cur_month > 11) {
|
|
|
|
cur_month -= 12;
|
|
|
|
cur_year += 1;
|
2013-06-19 16:10:26 -04:00
|
|
|
}
|
2015-01-02 08:12:59 -05:00
|
|
|
printgrid(cur_year, cur_month, fday, line);
|
2013-06-19 14:13:50 -04:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2013-06-19 17:24:34 -04:00
|
|
|
eprintf("usage: %s [-1] [-3] [-m] [-s] [-y] [-c columns]"
|
2015-01-06 15:14:56 -05:00
|
|
|
" [-f firstday] [-n nmonths] [ [month] year]\n",
|
2013-06-19 16:10:26 -04:00
|
|
|
argv0);
|
2013-06-19 14:13:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-01-06 15:14:56 -05:00
|
|
|
int year, month, ncols, nmons, fday;
|
2013-06-19 16:10:26 -04:00
|
|
|
struct tm *ltime;
|
|
|
|
time_t now;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
ltime = localtime(&now);
|
|
|
|
year = ltime->tm_year + 1900;
|
|
|
|
month = ltime->tm_mon + 1;
|
|
|
|
fday = 0;
|
2013-06-19 14:13:50 -04:00
|
|
|
|
2013-06-19 16:10:26 -04:00
|
|
|
ncols = 3;
|
|
|
|
nmons = 1;
|
2013-06-19 14:13:50 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2013-06-19 16:10:26 -04:00
|
|
|
case '1':
|
|
|
|
nmons = 1;
|
|
|
|
break;
|
|
|
|
case '3':
|
|
|
|
nmons = 3;
|
|
|
|
month -= 1;
|
|
|
|
if(month == 0) {
|
|
|
|
month = 12;
|
|
|
|
year--;
|
|
|
|
}
|
|
|
|
break;
|
2013-06-19 14:13:50 -04:00
|
|
|
case 'c':
|
2013-06-19 16:10:26 -04:00
|
|
|
ncols = estrtol(EARGF(usage()), 0);
|
2013-06-19 14:13:50 -04:00
|
|
|
break;
|
2013-06-19 16:10:26 -04:00
|
|
|
case 'f':
|
|
|
|
fday = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
case 'm': /* Monday */
|
|
|
|
fday = 1;
|
2013-06-19 14:13:50 -04:00
|
|
|
break;
|
|
|
|
case 'n':
|
2013-06-19 16:10:26 -04:00
|
|
|
nmons = estrtol(EARGF(usage()), 0);
|
|
|
|
break;
|
|
|
|
case 's': /* Sunday */
|
|
|
|
fday = 0;
|
2013-06-19 14:13:50 -04:00
|
|
|
break;
|
|
|
|
case 'y':
|
2013-06-19 16:10:26 -04:00
|
|
|
month = 1;
|
|
|
|
nmons = 12;
|
2013-06-19 14:13:50 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
switch (argc) {
|
2013-06-19 16:10:26 -04:00
|
|
|
case 2:
|
|
|
|
month = estrtol(argv[0], 0);
|
|
|
|
argv++;
|
|
|
|
case 1:
|
|
|
|
year = estrtol(argv[0], 0);
|
|
|
|
break;
|
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
2015-01-02 08:12:59 -05:00
|
|
|
if (ncols < 0 || month < 1 || month > 12 || nmons < 1 || fday < 0 || fday > 6) {
|
2013-06-19 14:13:50 -04:00
|
|
|
usage();
|
2013-06-19 16:10:26 -04:00
|
|
|
}
|
|
|
|
|
2015-01-06 15:14:56 -05:00
|
|
|
drawcal(year, month-1, ncols, nmons, fday);
|
2013-06-19 14:13:50 -04:00
|
|
|
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2013-06-19 14:13:50 -04:00
|
|
|
}
|