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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/* The 'libkal' library for date conversion.
* Copyright (C) 1996-1998 Petr Tomasek <tomasek@etf.cuni.cz>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
/*
* Divide/modulo.
*/
long kal_dv(long a, long b) {
long c;
c=a/b;
if (c<=0) if (b*c>a) c--;
return c;
}
long kal_md(long a, long b) {
return a-b*kal_dv(a,b);
}
/* Just not to have to type a lot. Internal. */
#define dv(a,b) kal_dv(a,b)
#define md(a,b) kal_md(a,b)
/*
* Internal.
*/
int s(int m, int p) {
const int t[]={0,31,59,90,120,151,181,212,243,273,304,334};
int b;
if (m==13) return 365; else {
m=md(m-1,12)+1; /* for stability reasons */
b=t[m-1];
if (m<3) b=b-p;
return b; }
}
/*
* Julianic calendar.
*/
long kal_conv_jul_jd(int d,int m,int y) {
long p1,p2,q;
p1=dv(y,4);
p2=md(y,4);
q=0;if (p2==0) q=1;
return d+s(m,q)+1461*p1+365*p2+1721058;
}
void kal_conv_jd_jul(long jd,int *d,int *m,int *y) {
long p1,p2,q1,q2,q;
p1=dv(jd-1721058,1461);
q1=md(jd-1721058,1461);
if (q1==0) {*d=1; *m=1; *y=4*p1; }
else {
p2=dv(q1-1,365);
q2=md(q1-1,365);
*y=4*p1+p2;
q=0; if (md(*y,4)==0) q=1;
*m=1;
while ((*m<12) && ((q2+1)>s(*m+1,q))) *m=*m+1;;
*d=q2+1-s(*m,q);
}
}
long kal_jul_east(int y) { /* Computes jd of easter sunday for given year. */
long p1,p2,q,ep,d;
ep=md(11*md(y,19)+14,30);
p1=dv(y,4);
p2=md(y,4);
q=0; if (p2==0) q=1;
d=50-ep+s(3,q)+1461*p1+365*p2+1721058;
return d+7-md(d+1,7);
}
/*
* Gregorianic calendar.
*/
long kal_conv_gre_jd(int d,int m,int y) {
long m1,m2,n1,n2,q;
m1=dv(y,400);
m2=dv(y,100);
n1=dv(y,4);
n2=md(y,4);
q=0; if (n2==0) q=1;
if (md(y,100)==0) q=0;
if (md(y,400)==0) q=1;
return d+s(m,q)+m1-m2+1461*n1+365*n2+1721060;
}
void kal_conv_jd_gre(long jd,int *d,int *m,int *y) {
long qq,a1,a2,b1,b2,c1,c2,d1,d2,q;
qq=jd-1721060;
a1=dv(qq,146097);
a2=md(qq,146097);
b1=dv(a2,36524);
b2=md(a2,36524);
c1=dv(b2,1461);
c2=md(b2,1461);
if ((c2==0) && ((b2>0) || (a2==0)) ) {
*d=1; *m=1; *y=400*a1+100*b1+4*c1; }
else {
d1=dv(c2-1,365);
d2=md(c2-1,365);
*y=400*a1+100*b1+4*c1+d1;
q=0;
if (md(*y,4)==0) q=1;
if (md(*y,100)==0) q=0;
if (md(*y,400)==0) q=1;
*m=1;
while ((*m<12) && ((d2+1)>s(*m+1,q))) *m=*m+1;
*d=d2+1-s(*m,q);
}
}
long kal_gre_east(int y) { /* Computes jd of easter sunday for given year. */
long p1,p2,q,ep,a,g,d,m1,m2;
a=md(y,19);
m1=dv(y,400);
m2=dv(y,100);
g=md(dv(y,300)+m1-m2+14,30);
ep=md(11*a+g,30);
if (ep==0) ep=1;
if ((ep==1) && (a>10)) ep=2;
p1=dv(y,4);
p2=md(y,4);
q=0; if (p2==0) q=1;
d=50-ep+s(3,q)+m1-m2+1461*p1+365*p2+1721060;
return d+7-md(d+1,7);
}
/*
* Arabic calendar.
*/
long kal_conv_ar_jd(int d,int m,int h) {
long h1,h2;
h1=dv(h-1,30);
h2=md(h-1,30);
return d+29*(m-1)+dv(m,2)+10631*h1+354*h2+dv(7*h2+8,19)+1948439;
}
void kal_conv_jd_ar(long jd,int *d,int *m,int *h) {
long h1,h2,q1,q2;
h1=dv(jd-1948440,10631);
q1=md(jd-1948440,10631);
h2=-1;
do h2++; while (354*h2+dv(7*h2+8,19)<=q1);
h2--;
*h=30*h1+h2+1;
q2=q1-(354*h2+dv(7*h2+8,19));
*m=0;
do *m=*m+1; while (29*(*m-1)+dv(*m,2)<=q2);
*m=*m-1;
*d=q2-(29*(*m-1)+dv(*m,2))+1;
}
/*
* Turkish calendar
*/
/*
* THIS IS (afaik) BUGGY !!!!!
*
long kal_conv_tur_jd(int d,int m,int h) {
long h1,h2;
h1=dv(h-1,8);
h2=md(h-1,8);
return d+29*(m-1)+dv(m,2)+2835*h1+354*h2+dv(3*h2+1,8)+1948440;
}
void kal_conv_jd_tur(long jd,int *d,int *m,int *h) {
long h1,h2,q1,q2;
h1=dv(jd-1948441,2835);
q1=md(jd-1948441,2835);
h2=-1;
do h2++; while (354*h2+dv(3*h2+1,8)<=q1);
h2--;
*h=8*h1+h2+1;
q2=q1-(354*h2+dv(3*h2+1,8));
*m=0;
do *m=*m+1; while (29*(*m-1)+dv(*m,2)<=q2);
*m=*m-1;
*d=q2-(29*(*m-1)+dv(*m,2))+1;
}
*
*
*/
/*
* Jewish calendar.
*/
int prest(int w2) {
int q=0;
if (w2>0) q=dv(7*w2+1,19)-dv(7*w2-6,19);
return q;
}
int uuw(int u2,long u3,int w2,int y) {
int uu=0;
switch (u2) {
case 2: case 4: case 6: uu=1; break;
case 1: if ((u3>=16404) && (prest(w2)==0)) uu=2; break;
case 0: if ((u3>=23269) && (prest(md(y-2,19))==1)) uu=1; break;
}
return uu;
}
long kal_jew_rosh_hashana(int y) {
long w1,w2,u2,w,uu,u1,u3;
w1=dv(y-1,19);
w2=md(y-1,19);
w=235*w1+12*w2+dv(7*w2+1,19);
u3=md(13753*w+12084,25920);
uu=29*w+dv(13753*w+12084,25920);
u2=md(uu,7);
u1=dv(uu,7);
return 347998+7*u1+u2+uuw(u2,u3,w2,y);
}
int z_s(int m,int p,int q) {
int ss;
ss=(m-1)*29;
if ((p==0) && (m>7)) ss=ss-29;
if (m>6) ss=ss+3+dv(m-7,2);
else ss=ss+dv(m,2);
if ((p==1) && (m>6)) ss++;
if ((q==0) && (m>3)) ss--;
if ((q==2) && (m>2)) ss++;
return ss;
}
long kal_conv_jew_jd(int d, int m, int y) {
long j1,j2;
int j,p,q;
j1=kal_jew_rosh_hashana(y);
j2=kal_jew_rosh_hashana(y+1);
j=j2-j1;
if (j>380) { p=1; q=j-383; }
else { p=0; q=j-353; }
return j1+z_s(m,p,q)+d-1;
}
void kal_conv_jd_jew(long jd,int *d, int *m, int *y) {
long w1,w2,ww,jd1,jd2;
int j,p,q;
/* ww=(long)((float)((jd-347998)/29.5305941)); */
ww=(((jd-347998)/29.5305941));
if (ww<0) ww--;
w1=dv(ww,235);
w2=dv(md(ww,235),12);
*y=19*w1+w2-1;
jd2=kal_jew_rosh_hashana(*y);
do {jd1=jd2; *y=*y+1; jd2=kal_jew_rosh_hashana(*y); } while (jd2<=jd);
*y=*y-1;
j=jd2-jd1;
if (j>380) {p=1; q=j-383;}
else {p=0; q=j-353;}
j=jd-jd1;*m=0;
do {*m=*m+1; } while (j>=z_s(*m,p,q));
*m=*m-1;
*d=j-z_s(*m,p,q)+1;
}
/* is the year leap? */
int kal_jew_leap( int y) {
return ((kal_jew_rosh_hashana(y+1)- /* Is the number of days */
kal_jew_rosh_hashana(y))>360); /* in the year > 360 ? */
}
/*
* Days of week
*/
int kal_day_of_week(long jd) {
return md(jd+1,7);
}
long kal_prev_dw (long jd, int dw) {
return (7*dv(jd+1-dw,7)+dw-1);
}
long kal_prev_before_dw (long jd, int dw) {
return (7*dv(jd-dw,7)+dw-1);
}
long kal_next_dw (long jd, int dw) {
return (dw-1-7*dv(dw-1-jd,7));
}
long kal_next_after_dw (long jd, int dw) {
return (dw-1-7*dv(dw-2-jd,7));
}
/*
* Year adjustments.
*
*/
int kal_year_code(int y,int a) {
if (a) return 1-y;
else return y;
}
int kal_year_decode(int y, int *a) {
if (y<1) { *a=1; return 1-y;}
else { *a=0; return y;}
}
|