home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
xbase
/
library
/
clipper
/
rettig
/
source
/
_tr_mkdt.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-10-21
|
3KB
|
115 lines
/*********
*
* _tr_mkdt.c
*
* by Tom Rettig
* Modified by Leonard Zerman
*
* Placed in the public domain by Tom Rettig Associates, 10/22/1990.
*
* Syntax: _tr_makedate( <year>, <month>, <day> )
* Return: valid <date string> of <year> plus <month> plus <day>
********/
#include "trlib.h"
char *_tr_makedate(year,month,day)
double year, month;
long int day;
{
static char ret[9] = " ";
int newyear, newmonth, newday, lastday,
mosmonth, mosday, monthyears, daymonths, calcmonth, calcday,
yrsyear, yrsday;
/* break into integer and decimal for calculating months and days */
mosmonth = (int) month;
/* consider current month when calculating day part of mos */
mosday = (int) ( (month - (double)mosmonth) *
((ISYEAR(year)&&ISMONTH((int)month))?
(double)_tr_ldm((int)month,(int)year):31) );
/* break into integer and decimal for converting fractional years to days */
yrsyear = (int) year;
yrsday = (int) ( (year - (double)yrsyear) * (ISLPYR(yrsyear)?366:365) );
/* add mos and yrs fractions to days */
day += (long int)(mosday + yrsday);
/* push excess days into months and excess months into years */
daymonths = (int)(day / ((ABS(day)>32 && (day%31)==1) ? 31 : 32));
calcday = (int)((day-1) % 31) +1;
mosmonth += daymonths;
calcmonth = ((mosmonth-1) % 12) +1;
if( mosmonth >= 0 )
{
monthyears = mosmonth /((mosmonth%12)==0 ? 13 : 12);
monthyears += (mosmonth%12==0) ? (int)(mosmonth/156) : 0;
}
else
{
monthyears = mosmonth /((mosmonth%13)==0 ? 13 : 12);
monthyears += (mosmonth%13==0) ? (int)(mosmonth/156) : 0;
}
monthyears += ((calcmonth==12) && (mosmonth%13==0)) ||
((calcmonth==1 ) && (mosmonth < -10)) ? -1 : 0;
newyear = yrsyear + monthyears;
/* calculate preliminary new month assuming 31 days */
if ( calcmonth > 0 )
newmonth = calcmonth;
else
{
newmonth = 12 + calcmonth;
newyear--;
}
/* calculate preliminary new day */
if ( calcday > 0 )
newday = calcday;
else
{
newday = 31 + calcday;
if ( newmonth == 1 )
{
newmonth = 12;
newyear--;
}
else
newmonth--;
}
/* if year is zero or less, date becomes 01/01/0001 */
if ( newyear <= 0 )
newyear = newmonth = newday = 1;
else
{
/* recalculate if newday exceeds valid lastday of month */
lastday = _tr_ldm(newmonth,newyear);
if ( newday > lastday )
{
if ( calcday > 0 )
{
/* will never go over 12 because December has 31 days */
newmonth++;
newday -= lastday;
}
else
newday = lastday;
}
}
/* build new date string */
ret[0] = DIGIT( newyear / 1000 );
ret[1] = DIGIT((newyear % 1000) / 100 );
ret[2] = DIGIT((newyear % 100) / 10 );
ret[3] = DIGIT( newyear % 10 );
ret[4] = DIGIT( newmonth / 10 );
ret[5] = DIGIT( newmonth % 10 );
ret[6] = DIGIT( newday / 10 );
ret[7] = DIGIT( newday % 10 );
ret[8] = NULLC;
return( ret );
}