/*	OXEGE7.C
**
**	Date and time routines.
*/

#include "a:stdio.h"
#include "b:oxege.h"

int day, month, year, jdate, hour, minute, second;
int dpm[13] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 400 };
int  dm[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

/* 	GET TIME FROM INTERNAL CLOCK
   	RETURNS INTEGERS hour, minute, second */
gettime()
	{
	int nt;
	char *c;
	c = HOURLOC;
	hour = *c;
	c = MINUTELOC;
	minute = *c;
	c = SECONDLOC;
	second = *c;
	
	hour = bcdbin(hour);
	minute = bcdbin(minute);
	second = bcdbin(second);

	}

/***  GET HOUR, MINUTE, OR SECOND FROM EXECUTIVE CLOCK   ***/
bcdbin( number )
	int number;
	{
	int n2;
	n2 = number / 16;
	n2  = ( n2 * 10) + (number - (16 * n2 ) );
	return n2;
	}

binbcd( number )
	int number;
	{
	itoa( number, scratch );
	return atoib( scratch, 16 );
	}


getdate()
	{
	int *iptr;
	iptr = DATELOC;
	jdate = *iptr;
	jtoc();
	}

ctoj()
	{
	int y;
	y = year - 1978;
	jdate 	= day			/* day of month 		*/ 
		+ dpm[ month - 1 ] 	/* days in previous months 	*/
		+ (365 * y) 		/* days in previous years	*/
		+ ((y + 1) / 4); 	/* previous leap days		*/
	if ( ( ((y + 2) % 4) == 0) && (month > 2) )
		{ ++jdate; }
	}

jtoc()
	{
	int days, pld, y;
	pld = (jdate + 671) / 1461;	/* previous leap days		*/
	y = (jdate - pld - 1) / 365;
	year = y + 1978;
	days = jdate - ((y * 365) + pld);	
	month = 0;
	while ( days > dpm[ month ] ) 
		{ ++month; }
	day = days - dpm[ month - 1 ]; 
	if ( ((jdate + 671) % 1461) == 0)
		{ day = 29; month = 2; }
	}

nwkday( jd )
	int jd;
	{
	int d;
	d = (jd % 7) - 1;
	if (d < 0) { d += 7; }
	return d;
	}

swkday( n )
	int n;
	{
	switch( n )
		{
		case 0:	return "Sunday";
			break;
		case 1:	return "Monday";
			break;
		case 2:	return "Tuesday";
			break;
		case 3:	return "Wednesday";
			break;
		case 4:	return "Thursday";
			break;
		case 5:	return "Friday";
			break;
		case 6:	return "Saturday";
			break;
		}
	}

smonth( n )
	int n;
	{
	switch( n )
		{
		case 1:	return "JANUARY";
			break;
		case 2:	return "FEBRUARY";
			break;
		case 3:	return "MARCH";
			break;
		case 4:	return "APRIL";
			break;
		case 5:	return "MAY";
			break;
		case 6: return "JUNE";
			break;
		case 7:	return "JULY";
			break;
		case 8:	return "AUGUST";
			break;
		case 9:	return "SEPTEMBER";
			break;
		case 10: return "OCTOBER";
			break;
		case 11: return "NOVEMBER";
			break;
		case 12: return "DECEMBER";
			break;
		}
	}	

 return "OCTOBER";
			break;
		case 11: return "NOVEMBER";
			break;
		case 12: return "DECEMBER";
			break;
		}