
/*
***                         --- CGCalendar v1.0 ---
**
***                 Copyright Craig G. Callan (07.30.94)
**
*/

#include <stdio.h>
#include <string.h>
#include <time.h>

    const       earliest = 1991, offset = 0;

    int         day, julian, leapflg, month, wkday, wkdayfirst, year;
    int         monthlen [12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    char        buf [80], yr [4], wkname [10], weekname [8][10], mname [12], monthname [13][12];
    time_t      timeDate;
    struct tm   *localTime;
    char        datestring[80];

void main ()
{
    int     i, j;

    initialize ();
    leapflg = checkforleapyear (year);
    julian = findjulian (day, month);
    drawcal ();
}

/*
***
*/

int checkforleapyear (yr)
{
    if ((yr % 4) == 0)
    {                     /* Check for leap year and alter February MONTHLEN */
        monthlen [2] = 29;
        return 1;
    }
    else
    {                     /* Also set LEAPFLG to TRUE or FALSE as needed     */
        monthlen [2] = 28;
        return 0;
    }
}

drawcal ()
{
    int     i, j;

    puts (" ");
    strcopy (buf, monthname [month]); strcat (buf, " "); strcat (buf, yr);
    if (leapflg == 1) strcat (buf, " *");
    j = 13-((strlen (buf) + 6)/2);
    for (i = 1; i < j; i++)
        printf (" ");
    printf ("--- %s ---\n", buf);
    j = wkdayfirst;
    for (i = 1; i < j; i++)
        printf ("    ");
    for (i = 1; i < (monthlen [month - 1] + 1); i++)
    {
        if (i < 10)
        {
            if (i != day)
                printf ("  %d ", i);
            else
                printf (" [%d]", i);
        }
        else
        {
            if (i != day)
                printf (" %d ", i);
            else
                printf ("[%d]", i);
        }
        j++;
        if (j > 7)
        {
            j = 1;
            printf ("\n");
        }
    }
    printf ("\n\n");
}

int findjulian (dy, mnth) /* Returns the julian date given the day and month */
{
    int     jul, i;

    jul = dy;
    for (i = 1; i < mnth - 1; i++)
        jul = jul + monthlen [i];
    return jul;
}

initialize ()
{
    int         i, j;
    char        buf [4];

    strcopy (weekname [1], "Sunday"); strcopy (weekname [2], "Monday");
    strcopy (weekname [3], "Tuesday"); strcopy (weekname [4], "Wednesday");
    strcopy (weekname [5], "Thursday"); strcopy (weekname [6], "Friday");
    strcopy (weekname [7], "Saturday");
    strcopy (monthname [1], "January"); strcopy (monthname [2], "February");
    strcopy (monthname [3], "March"); strcopy (monthname [4], "April");
    strcopy (monthname [5], "May"); strcopy (monthname [6], "June");
    strcopy (monthname [7], "July"); strcopy (monthname [8], "August");
    strcopy (monthname [9], "September"); strcopy (monthname [10], "October");
    strcopy (monthname [11], "November"); strcopy (monthname [12], "December");
    timeDate = time (NULL);
    localTime = localtime (&timeDate);
    strftime (datestring, (size_t)80, "\"%A, %d %B %Y\"\n", localTime );
    i = 0;
    while (datestring [i] != 'y' && datestring [i] != 0)
    {
        wkname [i] = datestring [i + 1];
        i++;
        wkname [i] = 0;
    }
    wkday = 0;
    for (j = 1; j < 8; j++)
        if (strcmp (weekname [j], wkname) == 0) wkday = j;
    i = i + 3; j = 0;
    while (datestring [i] != ' ' && datestring [i] != 0)
    {
        buf [j] = datestring [i];
        i++; j++;
        buf [j] = 0;
    }
    day = atoi (buf);
    i++; j = 0;
    while (datestring [i] != ' ' && datestring [i] != 0)
    {
        mname [j] = datestring [i];
        i++; j++;
        mname [j] = 0;
    }
    month = 0;
    for (j = 1; j < 13; j++)
        if (strcmp (monthname [j], mname) == 0) month = j;
    i++; j = 0;
    while (j < 4)
    {
        yr [j] = datestring [i];
        i++; j++;
        yr [j] = 0;
    }
    year = atoi (yr);
    i = day; wkdayfirst = wkday;
    while (i > 1)
    {
        i--; wkdayfirst--;
        if (wkdayfirst < 1) wkdayfirst = 7;
    }
}

strcopy (t, s)  register    char    *t, *s;
{
    while (*t++ = *s++);
}
