campo-sirio/gfm/adate365.c
alex ba237a9d91 Patch level : no patch
Files correlati     :
Ricompilazione Demo : [ ]
Commento            :
Aggiunti i sorgenti per Greenleaf Math Library (gfm.dll)


git-svn-id: svn://10.65.10.50/trunk@10079 c028cbd2-c16b-5b4b-a496-9718f37d4682
2002-02-26 12:19:02 +00:00

145 lines
2.7 KiB
C
Executable File

/* void AddDaysToDate365( m2, d2, y2, m1, d1, y1, ndays )
*
* ARGUMENTS
* int m1, d1, y1 - Specify a base date.
* int *m2, *d2, *y2 - Specify a date to be calculated
* int ndays - Number of days (+ or -) to be added to base
*
* DESCRIPTION
* A signed number of days (ndays) is added to the base date to form a
* second date which is transferred to the variables m2,d2,y2. If any of
* the arguments are NULL on input, an error results.
*
* Years < 100 are taken to be in 20th century.
*
* Years are considered to have 365 days. Leap years are considered here.
*
* SIDE EFFECTS
* Modified d2, m2, y2.
*
* RETURNS
* None.
*
* POSSIBLE ERROR CODES
*
* GM_ARGVAL
* GM_NULLPOINTER
*
* AUTHOR
* Don Killen 03-Feb-1988 16:27:32
* Copyright (C) 1988-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*
*/
#include <stdio.h>
#include "gm.h"
#include "gmsystem.h"
void AddDaysToDate365(m2,d2,y2,m1,d1,y1,ndays)
int m1,*m2,d1,*d2,y1,*y2,ndays;
{
long n1;
int np, x, z, mt, dt=1, dp, ny, t1, t2;
mbool ly;
_MacStart(GM_ADATE365);
if (d2==NULL||m2==NULL||y2==NULL) {
_MacErr(GM_NULLPOINTER);
_MacRetV;
}
if ( m1<1 || m1>12 || d1<1 || d1>31 || y1<1 || y1>9999 ) {
_MacErr(GM_ARGVAL);
_MacRetV;
}
if (m1<=2) {
x=0;
z=y1-1;
}
else {
x=(4*m1 + 23) / 10;
z=y1;
}
/* compute day # */
n1 = (long) y1 * 365L + (long) (31*(m1-1) + d1 + z/4 - x)
+(long) ndays;
/* estimate year */
*y2 = (int) ((4L * n1) / 1461L);
np = (int) (n1 - ((long) (*y2) * 365L + (long) (((*y2) - 1) / 4)));
/* correct year estimate */
while (np<1) { /* year too large */
*y2 = (*y2)-1;
np = (int) (n1 - ((long)(*y2) * 365L +
(long) (((*y2)-1) / 4)));
}
if ( ((*y2) % 4) == 0)
ny = 366;
else
ny = 365;
while (np > ny ) { /* year too small */
*y2 = (*y2)+1;
np = (int) (n1 - ((long)(*y2) * 365L +
(long) (((*y2)-1) / 4)));
}
/* no leap year in 1700, 1800, 1900 */
if ((y1>=1700 && y1<=1900) || (*y2>=1700 && *y2<=1900)) {
t1 = (m1<=2) ? y1-1 : y1;
t2 = (np<60) ? (*y2)-1 : *y2;
if (np==60 && ndays<0)
t2--;
/* if y1 and y2 fall on different centuries, extra leap year counted */
np+= ((t2/100) - (t1/100));
if (np<1) {
*y2 = *y2-1;
*m2 = 12;
*d2 = 31;
_MacRetV;
}
if (np>366) {
*y2 = *y2+1;
*m2 = 1;
*d2 = 1;
_MacRetV;
}
}
/* handle Jan. & Feb. */
if (np <= 31) {
*m2 = 1;
*d2 = np;
_MacRetV;
}
/* check for leap year */
ly = ((*y2) % 4 == 0);
if (np <= 59 + (ly)) {
*m2 = 2;
*d2 = np - 31;
_MacRetV;
}
/* handle other months */
if (ly)
np--;
mt = 2;
do {
mt++;
dp = dt;
dt = np - 31 * (mt - 1) + (4 * mt + 23) / 10;
} while (dt>=1);
*m2 = mt-1;
*d2 = dp;
_MacRetV;
}