/* addays.c
 *
 * converts a given "int date[3]" and a number of "int days" into 
 * "int pdate[3]" "days" later than "date[]".
 *
 * Modified 08/04/94:
 * 1) output zero padded for a date output of exactly
 *	2 + 1 + 2 + 1 + 2 = 8 chars.
 * 2) silent option -s that only outputs result with no newline.
 */

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

#define YEAR 365
#define CENTURY 1900

static int mon[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},
			 {0,31,29,31,30,31,30,31,31,30,31,30,31}};


static char _sccsid[] = { " %M% %I% %H% " };

int main( argc, argv )
int argc;
char **argv;
{
	void usg();

        int date[3];
        int days;
        int pdate[3];
        int leap, tleap;
        int silent, Ione, Itwo;
        char slash1, slash2;
        int yr;
        int i;

	silent = 0;

        if( argc != 3 && argc != 4 )   {
		usg();
	}
	if( strcmp( argv[1], "-s" ) == 0 )    {
		silent = 1;
		Ione = 2;
		Itwo = 3;
	}
	else    {
		Ione = 1;
		Itwo = 2;
	}

	if( 5 != sscanf( argv[Ione], "%d%c%d%c%d", &date[0], &slash1, &date[1], &slash2, &date[2] ) )    {
			fprintf( stderr,"addays: can't read entered date\n" );
			if( slash1 != '/' || slash2 != '/' )    {
				fprintf( stderr, "Slash missing\n" );
			}
			exit( 1 );
	}
        if( 1 != sscanf( argv[Itwo], "%d", &days ) )    {
                fprintf( stderr, "addays: can't read number of days\n" );
                exit( 1 );
        }

        if( date[0] > 12 )   {
                fprintf( stderr, "addays: month parameter out of bounds\n" );
                exit( 1 );
        }

        tleap = date[2]%4 == 0 && date[2]%100 != 0 || date[2]%400 == 0;
        if( date[1] > mon[tleap][date[0]] )   {
                fprintf( stderr, "addays: date out of bounds for month\n");
                exit( 1 );
        }

        yr = date[2] + CENTURY;
        leap = yr%4 == 0 && yr%100 != 0 || yr%400 == 0;

        if( days <= mon[leap][date[0]] )   {
                pdate[1] = date[1] + days;
                if( pdate[1] > mon[ leap ][ date[0] ] )  {
                        pdate[0] = date[0] + 1;
                                if( pdate[0] == 13 )   {
                                        pdate[0] = 1;
                                        pdate[2] = date[2] + 1;
                                        yr = pdate[2];
					leap=yr%4==0&&yr%100!=0||yr%400==0;
                                }
				else    {
					pdate[2] = date[2];
				}
                        pdate[1] -= mon[ leap ][ date[0] ];
                }
                else {
                        pdate[0] = date[0];
                        pdate[2] = date[2];
                }
        }

        else {
                while( days > YEAR+leap )   {
                        ++yr;
                        leap = yr%4 == 0 && yr%100 != 0 || yr%400 == 0;
                        days -= YEAR + leap;
                }
                leap = yr%4 == 0 && yr%100 != 0 || yr%400 == 0;
                days += date[1];
                for( i = date[0]; days > mon[leap][i]; i++ )   {
                        days -= mon[leap][i];
                        if( i == 12)   {
                                i = 0;
                                ++yr;
				leap=yr%4==0&&yr%100!=0||yr%400==0;
                        }
                }
                pdate[0] = i;
                pdate[1] = days;
                pdate[2] = yr - CENTURY;

        }
        if( pdate[2] >= 100 )   {
                pdate[2] = pdate[2] - 100;
                fprintf( stderr, "Date is in the twenty first century\n");
        }

	/* ----------------------------------------------------------- */

	if( silent )    {
		printf( "%02d/%02d/%02d", pdate[0], pdate[1], pdate[2] );
	}
	else    {
		printf( "\tThe date %s days after date %02d/%02d/%02d is ", argv[Itwo], date[0], date[1], date[2] );
		printf( "%02d/%02d/%02d.\n", pdate[0], pdate[1], pdate[2] );
	}

	return( 0 );

} /* end addays.c */

void usg()
{
	fprintf( stderr, "Usage: addays [-s] mn/dy/yr days\n" );
	fprintf( stderr, "Output years less than entered year are in the" );
	fprintf( stderr, " twenty first century\n" );
	exit( 1 );
}


Return to Home Page
Return to Metayoga Page
Return to C Language Page


The URL for this document is:
http://graham.main.nc.us/~bhammel/graham/CPROGS/addays.html
Created: 1997
Last Updated: May 28, 2000 Email me, Bill Hammel at
bhammel@graham.main.nc.us
READ WARNING BEFORE SENDING E-MAIL