
/* define the time structure for binary time variables */
struct rtc_time
{
    unsigned char sec;
    unsigned char min;
    unsigned char hour;
    unsigned char date;
    unsigned char mon;
    unsigned char year;
};

/* global current time structure */
extern struct rtc_time curr_time;

/* table of normal year days per month */
unsigned int rtc_mondays[]={31,28,31,30,31,30,31,31,30,31,30,31};

/*
**
** routine to software increment the date in the rtc_time structure
** pointed to by the entry pointer
**
*/

void date_inc(struct rtc_time *tp)
{
    tp->date++;
    if(tp->date > rtc_mondays[tp->mon-1])
    {
        if((tp->mon == 2) && ((tp->year & 0x03) == 0) && (tp->date == 29))
            return;            /* allow keeping leap year day */
        tp->date=1;
        tp->mon++;
        if(tp->mon > 12)
        {
            tp->mon=1;
            tp->year++;
            if(tp->year > 99)
            {
                tp->year=0;
            }
        }
    }
}

/*
**
** routine to software decrement the date in the rtc_time structure
** pointed to by the entry pointer
**
*/

void date_dec(struct rtc_time *tp)
{
    tp->day--;
    if(tp->day == 0)
        tp->day=7;              /* reset the day number to saturday */
    tp->date--;
    if(tp->date == 0)
    {
        tp->mon--;
        if(tp->mon == 0)
        {
            tp->mon=12;         /* reset to december */
            if(tp->year == 0)
            {
                tp->year=99;
            }
            else
            {
                tp->year--;
            }
        }
        else
        {
            if((tp->mon == 2) && ((tp->year & 0x03) == 0))
            {
                tp->date++;     /* extra day for feb */
            }
        }
        tp->date+=rtc_mondays[tp->mon-1];
    }
}
