/**************************************************************/
/* */
/* (c) Copyright 1993 by Stan Milam. */
/* */
/**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include "dates.h"
#define MAXDATE 3652059L /* 31-Dec-9999 */
/**************************************************************/
/* A table which contains an accumulation of days of all */
/* preceeding months. */
/**************************************************************/
static int month_accum_table[] = {
0, 31, 59, 90, 120, 151,
181, 212, 243, 273, 304, 334
};
/**************************************************************/
/* Each element contains the normal number of days for each */
/* month. */
/**************************************************************/
static int month_table[] = {
31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
};
/**************************************************************/
/* The full names of the weekdays. */
/**************************************************************/
static char *full_weekday[] = {
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"
};
/**************************************************************/
/* The full names of the months. */
/**************************************************************/
static char *full_month[] = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};
static int compare( char *s1, char *s2, unsigned len ) {
unsigned ch1, ch2;
while ( *s1 && *s2 && len ) {
ch1 = toupper(*s1);
ch2 = toupper(*s2);
if (ch1 < ch2)
return -1;
else if ( ch1 > ch2 )
return 1;
else s1++, s2++, len--;
}
if ( len == 0 ) return 0;
else if (*s1) return 1;
else return -1;
}
static int is_it_a_leap_year( unsigned year ) {
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0
);
}
static date_t years_to_days( unsigned year ) {
date_t rv;
if (year > 0) year--;
rv = year * 365L + year / 4L - year / 100L + year / 400L;
return rv;
}
static date_t months_to_days( int month, int leap_year ) {
date_t rv;
rv = month_accum_table[month - 1];
if ( month > 2 ) rv += leap_year;
return rv;
}
static int days_to_months( int *days, int leap_year ) {
int rv, month, save_month;
save_month = month_table[1];
month_table[1] += leap_year;
for ( month = 0 ;; month++ ) { /* Go find the */
/* month and day */
if ( *days > month_table[month] ) /* More days than */
/* in month? */
*days -= month_table[month]; /* Yes, subtract */
/* from days */
else {
rv = month; /* Found month, */
/* days left over */
break; /* Get out of loop */
}
}
month_table[1] = save_month;
return rv; /* Return month */
}
/**************************************************************/
/* week_of_year() */
/* */
/* NOTE! This function was borrowed from P. J. Plauger's book */
/* "The Standard C Library". */
/* */
/**************************************************************/
static int week_of_year( int start, int wday, int yday ) {
wday = ( wday + 7 - start ) % 7;
return ( yday - wday + 12 ) / 7 - 1;
}
date_t time_to_date( time_t tv ) {
date_t rv;
struct tm *tm;
int year, leap_year;
/**********************************************************/
/* Get a time structure to use for conversion process. */
/**********************************************************/
tm = localtime(&tv);
/**********************************************************/
/* Use values in the tm structure to convert the current */
/* date into a long integer value. */
/**********************************************************/
year = tm -> tm_year + 1900;
leap_year = is_it_a_leap_year(year);
rv = years_to_days( year );
rv += months_to_days(tm -> tm_mon + 1, leap_year );
rv += tm -> tm_mday;
return rv;
}
/* End of File */