1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
#include <stdio.h>
#define J(date) ((date) % 100) //aaaammJJ
#define M(date) (((date) / 100) % 100)//aaaaMMjj
#define A(date) ((date) / 10000) //AAAAmmjj
int jma2jj(int j, int m, int a)
{ //Conversion j m a en jour julien
return (1461 * (a + 4800 + (m - 14) / 12)) / 4 + (367 * (m - 2 - 12 * ((m - 14) / 12))) / 12 - (3 * ((a + 4900 + (m - 14) / 12) / 100)) / 4 + j - 32075;
}
void jj2jma(int n, int *j, int *m, int *a)
{ //Conversion jour julien en j m a
int t0 = n + 68569;
int t1 = (4 * t0) / 146097;
int t2 = t0 - (146097 * t1 + 3) / 4;
int t3 = (4000 * (t2 + 1)) / 1461001;
int t4 = t2 - (1461 * t3) / 4 + 31;
int t5 = (80 * t4) / 2447;
*j = t4 - (2447 * t5) / 80;
*m = t5 + 2 - (12 * (t5 / 11));
*a = 100 * (t1 - 49) + t3 + (t5 / 11);
}
int calcdate(int date, int n)
{ //Retourne date +- n jours
int j, m, a;
jj2jma(jma2jj(J(date), M(date), A(date)) + n, &j, &m, &a);
return a * 10000 + m * 100 + j;
}
int main(void)
{
int n, date = 20121001; //01/10/2012
for(n = -100000; n; n /= 10)
printf("%d%-+7d=%d\n", date, n, calcdate(date, n));
for(n = 1; n <= 100000; n *= 10)
printf("%d%-+7d=%d\n", date, n, calcdate(date, n));
return 0;
} |
Partager