| 1 |
/************************************************************************ |
| 2 |
* Copyright (c) 2004, Arabeyes, Fayez Alhargan |
| 3 |
* |
| 4 |
* This is a program that computes the Hijary dates for Umm-AlQura |
| 5 |
* calendar the official calendar of the Kingdom of Saudi Arabia. |
| 6 |
* |
| 7 |
* King Abdulaziz City for Science and Technology |
| 8 |
* Computer and Electronics Research Institute |
| 9 |
* Riyadh, Saudi Arabia |
| 10 |
* alhargan-at-kacst.edu.sa |
| 11 |
* Tel:4813770 Fax:4813764 |
| 12 |
* |
| 13 |
* Fayez Alhargan version: opn1.2 |
| 14 |
* Fayez Alhargan last modified 22-1-2003 |
| 15 |
* |
| 16 |
* (www.arabeyes.org - under LGPL license - see COPYING file) |
| 17 |
************************************************************************/ |
| 18 |
|
| 19 |
#include <stdlib.h> |
| 20 |
#include <math.h> |
| 21 |
|
| 22 |
#include "hijri.h" |
| 23 |
|
| 24 |
#define HStartYear 1420 |
| 25 |
#define HEndYear 1450 |
| 26 |
|
| 27 |
extern char *g_day[7]; |
| 28 |
extern char *h_day[7]; |
| 29 |
extern char *g_day_short[7]; |
| 30 |
extern char *h_day_short[7]; |
| 31 |
extern char *g_month[13]; |
| 32 |
extern char *h_month[13]; |
| 33 |
extern char *g_month_short[13]; |
| 34 |
extern char *h_month_short[13]; |
| 35 |
|
| 36 |
int MonthMap[]={19410, |
| 37 |
19396,19337,19093,13613,13741,15210,18132,19913,19858,19110, |
| 38 |
18774,12974,13677,13162,15189,19114,14669,13469,14685,12986, |
| 39 |
13749,17834,15701,19098,14638,12910,13661,15066,18132,18085 |
| 40 |
}; |
| 41 |
|
| 42 |
/* makes it circular m[0]=m[12] & m[13]=m[1] */ |
| 43 |
short gmonth[14]={31,31,28,31,30,31,30,31,31,30,31,30,31,31}; |
| 44 |
|
| 45 |
/* makes it circular m[0]=m[12] & m[13]=m[1] */ |
| 46 |
short smonth[14]={31,30,30,30,30,30,29,31,31,31,31,31,31,30}; |
| 47 |
|
| 48 |
|
| 49 |
/* Various Prototypes */ |
| 50 |
int G2H(sDate *mydate, int dg, int mg, int yg); |
| 51 |
int H2G(sDate *mydate, int dh, int mh, int yh); |
| 52 |
int Hsub2G(sDate *mydate, int mh, int yh); |
| 53 |
|
| 54 |
double GCalendarToJD(int yg, int mg, double dg ); |
| 55 |
double JDToGCalendar(double JD, sDate *mydate); |
| 56 |
int GLeapYear(int year); |
| 57 |
|
| 58 |
void GDateAjust(sDate *mydate); |
| 59 |
int DayWeek(long JulianD); |
| 60 |
|
| 61 |
void JDToHCalendar(double JD, sDate *mydate); |
| 62 |
void JDToHACalendar(double JD, int *yh, int *mh, int *dh); |
| 63 |
double HCalendarToJD(int yh, int mh, int dh); |
| 64 |
double HCalendarToJDA(int yh, int mh, int dh); |
| 65 |
int HMonthLength(int yh, int mh); |
| 66 |
|
| 67 |
double ip(double x); |
| 68 |
int mod(double x, double y); |
| 69 |
int IsValid(int yh, int mh, int dh); |
| 70 |
|
| 71 |
|
| 72 |
/****************************************************************************/ |
| 73 |
/* Name: Hsub2G */ |
| 74 |
/* Type: Procedure */ |
| 75 |
/* Purpose: Finds Gdate(year,month,day) for Hdate(year,month,day) */ |
| 76 |
/* Arguments: */ |
| 77 |
/* Input: Hijri date: year:yh, month:mh */ |
| 78 |
/* Output: Gregorian date: year:yg, month:mg, day:dg , day of week:dayweek */ |
| 79 |
/* and returns flag found:1 not found:0 */ |
| 80 |
/****************************************************************************/ |
| 81 |
int Hsub2G(sDate *mydate, int mh, int yh) |
| 82 |
{ |
| 83 |
int flag; |
| 84 |
long JD; |
| 85 |
double GJD; |
| 86 |
|
| 87 |
/* Make sure that the date is within the range of the tables */ |
| 88 |
if(mh < 1) {mh = 12;} |
| 89 |
if(mh > 12) {mh = 1;} |
| 90 |
if(yh < HStartYear) {yh = HStartYear;} |
| 91 |
if(yh > HEndYear) {yh = HEndYear;} |
| 92 |
|
| 93 |
GJD = HCalendarToJDA(yh, mh, 1); |
| 94 |
JDToGCalendar(GJD, mydate); |
| 95 |
JD = (long) GJD; |
| 96 |
mydate->weekday = (JD + 1) % 7; |
| 97 |
/* NOTE: so when is flag = 0 ? */ |
| 98 |
flag = 1; /* date has been found */ |
| 99 |
|
| 100 |
return(flag); |
| 101 |
} |
| 102 |
|
| 103 |
/****************************************************************************/ |
| 104 |
/* Name: HCalendarToJDA */ |
| 105 |
/* Type: Function */ |
| 106 |
/* Purpose: convert Hdate(year,month,day) to Exact Julian Day */ |
| 107 |
/* Arguments: */ |
| 108 |
/* Input : Hijrah date: year:yh, month:mh, day:dh */ |
| 109 |
/* Output: The Exact Julian Day: JD */ |
| 110 |
/****************************************************************************/ |
| 111 |
double HCalendarToJDA(int yh, int mh, int dh) |
| 112 |
{ |
| 113 |
int flag, Dy, m, b; |
| 114 |
long JD; |
| 115 |
double GJD; |
| 116 |
|
| 117 |
/* estimate JD of the beginning of year */ |
| 118 |
JD = (long) HCalendarToJD(yh, 1, 1); |
| 119 |
Dy = MonthMap[yh-HStartYear]/4096; /* Mask 1111000000000000 */ |
| 120 |
GJD = JD - 3 + Dy; /* correct the JD value from stored tables */ |
| 121 |
b = MonthMap[yh - HStartYear]; |
| 122 |
b = b - Dy * 4096; |
| 123 |
for(m=1; m < mh; m++) |
| 124 |
{ |
| 125 |
flag = b % 2; /* Mask for the current month */ |
| 126 |
if(flag) |
| 127 |
Dy = 30; |
| 128 |
else |
| 129 |
Dy = 29; |
| 130 |
GJD = GJD + Dy; /* Add the months lengths before mh */ |
| 131 |
b = (b - flag) / 2; |
| 132 |
} |
| 133 |
GJD = GJD + dh - 1; |
| 134 |
|
| 135 |
return(GJD); |
| 136 |
} |
| 137 |
|
| 138 |
/****************************************************************************/ |
| 139 |
/* Name: HMonthLength */ |
| 140 |
/* Type: Function */ |
| 141 |
/* Purpose: Obtains the month length */ |
| 142 |
/* Arguments: */ |
| 143 |
/* Input : Hijrah date: year:yh, month:mh */ |
| 144 |
/* Output: Month Length */ |
| 145 |
/****************************************************************************/ |
| 146 |
int HMonthLength(int yh, int mh) |
| 147 |
{ |
| 148 |
int flag, Dy, m, b; |
| 149 |
|
| 150 |
if(yh<HStartYear || yh>HEndYear) |
| 151 |
{ |
| 152 |
flag = 0; |
| 153 |
Dy = 0; |
| 154 |
} |
| 155 |
else |
| 156 |
{ |
| 157 |
Dy = MonthMap[yh - HStartYear]/4096; /* Mask 1111000000000000 */ |
| 158 |
b = MonthMap[yh - HStartYear]; |
| 159 |
b = b - Dy * 4096; |
| 160 |
for(m=1; m <= mh; m++) |
| 161 |
{ |
| 162 |
flag = b % 2; /* Mask for the current month */ |
| 163 |
if(flag) |
| 164 |
Dy = 30; |
| 165 |
else |
| 166 |
Dy = 29; |
| 167 |
b = (b - flag) / 2; |
| 168 |
} |
| 169 |
} |
| 170 |
return(Dy); |
| 171 |
} |
| 172 |
|
| 173 |
/****************************************************************************/ |
| 174 |
/* Name: DayInYear */ |
| 175 |
/* Type: Function */ |
| 176 |
/* Purpose: Obtains the day number in the yea */ |
| 177 |
/* Arguments: */ |
| 178 |
/* Input : Hijrah date: year:yh, month:mh day:dh */ |
| 179 |
/* Output: Day number in the Year */ |
| 180 |
/****************************************************************************/ |
| 181 |
int DayinYear(int yh, int mh, int dh) |
| 182 |
{ |
| 183 |
int flag, Dy, m, b, DL; |
| 184 |
|
| 185 |
if(yh<HStartYear || yh>HEndYear) |
| 186 |
{ |
| 187 |
flag = 0; |
| 188 |
DL = 0; |
| 189 |
} |
| 190 |
else |
| 191 |
{ |
| 192 |
Dy = MonthMap[yh - HStartYear]/4096; /* Mask 1111000000000000 */ |
| 193 |
b = MonthMap[yh - HStartYear]; |
| 194 |
b = b - Dy * 4096; |
| 195 |
DL = 0; |
| 196 |
for(m = 1; m <= mh; m++) |
| 197 |
{ |
| 198 |
flag = b % 2; /* Mask for the current month */ |
| 199 |
if(flag) |
| 200 |
Dy = 30; |
| 201 |
else |
| 202 |
Dy = 29; |
| 203 |
b = (b - flag) / 2; |
| 204 |
DL = DL + Dy; |
| 205 |
} |
| 206 |
DL = DL + dh; |
| 207 |
} |
| 208 |
return(DL); |
| 209 |
} |
| 210 |
|
| 211 |
/****************************************************************************/ |
| 212 |
/* Name: HYearLength */ |
| 213 |
/* Type: Function */ |
| 214 |
/* Purpose: Obtains the year length */ |
| 215 |
/* Arguments: */ |
| 216 |
/* Input : Hijrah date: year:yh */ |
| 217 |
/* Output: Year Length */ |
| 218 |
/****************************************************************************/ |
| 219 |
int HYearLength(int yh) |
| 220 |
{ |
| 221 |
int flag, Dy, m, b, YL; |
| 222 |
|
| 223 |
if(yh<HStartYear || yh>HEndYear) |
| 224 |
{ |
| 225 |
flag = 0; |
| 226 |
YL = 0; |
| 227 |
} |
| 228 |
else |
| 229 |
{ |
| 230 |
Dy = MonthMap[yh - HStartYear]/4096; /* Mask 1111000000000000 */ |
| 231 |
b = MonthMap[yh - HStartYear]; |
| 232 |
b = b - Dy * 4096; |
| 233 |
flag = b % 2; /* Mask for the current month */ |
| 234 |
if(flag) |
| 235 |
YL = 30; |
| 236 |
else |
| 237 |
YL = 29; |
| 238 |
|
| 239 |
for(m = 2; m <= 12; m++) |
| 240 |
{ |
| 241 |
flag = b % 2; /* Mask for the current month */ |
| 242 |
if(flag) |
| 243 |
Dy = 30; |
| 244 |
else |
| 245 |
Dy = 29; |
| 246 |
b = (b - flag) / 2; |
| 247 |
YL = YL + Dy; |
| 248 |
} |
| 249 |
} |
| 250 |
return(YL); |
| 251 |
} |
| 252 |
|
| 253 |
/****************************************************************************/ |
| 254 |
/* Name: G2H */ |
| 255 |
/* Type: Procedure */ |
| 256 |
/* Purpose: convert Gdate(year,month,day) to Hdate(year,month,day) */ |
| 257 |
/* Arguments: */ |
| 258 |
/* Input: Gregorian date: year:yg, month:mg, day:dg */ |
| 259 |
/* Output: Hijrah date: year:yh, month:mh, day:dh, day of week:dayweek */ |
| 260 |
/* and returns flag found:1 not found:0 */ |
| 261 |
/****************************************************************************/ |
| 262 |
int G2H(sDate *mydate, int dg, int mg, int yg) |
| 263 |
{ |
| 264 |
int yh2, mh2; |
| 265 |
int df; |
| 266 |
int flag = 1; |
| 267 |
long J; |
| 268 |
double GJD, HJD; |
| 269 |
sDate tmpdate; |
| 270 |
sDate tmpdate2; |
| 271 |
/*int error_fill;*/ |
| 272 |
|
| 273 |
GJD = GCalendarToJD(yg, mg, dg + 0.5); /* find JD of Gdate */ |
| 274 |
|
| 275 |
/* estimate the Hdate that correspond to the Gdate */ |
| 276 |
JDToHCalendar(GJD, &tmpdate); |
| 277 |
|
| 278 |
/* get the exact Julian Day */ |
| 279 |
HJD = HCalendarToJDA(tmpdate.year, tmpdate.month, tmpdate.day); |
| 280 |
df = (int) (GJD - HJD); |
| 281 |
tmpdate.day += df; |
| 282 |
while(tmpdate.day > 30) |
| 283 |
{ |
| 284 |
tmpdate.day -= HMonthLength(tmpdate.year, tmpdate.month); |
| 285 |
tmpdate.month++; |
| 286 |
if(tmpdate.month > 12) |
| 287 |
{ |
| 288 |
tmpdate.year++; |
| 289 |
tmpdate.month = 1; |
| 290 |
} |
| 291 |
} |
| 292 |
if(tmpdate.day == 30) |
| 293 |
{ |
| 294 |
yh2 = tmpdate.year; |
| 295 |
mh2 = tmpdate.month + 1; |
| 296 |
if(mh2 > 12) |
| 297 |
{ |
| 298 |
yh2++; |
| 299 |
mh2 = 1; |
| 300 |
} |
| 301 |
Hsub2G(&tmpdate2, mh2, yh2); |
| 302 |
|
| 303 |
/* Make sure that the month is 30days if not make adjustment */ |
| 304 |
if(dg == tmpdate2.day) |
| 305 |
{ |
| 306 |
tmpdate.year = yh2; |
| 307 |
tmpdate.month = mh2; |
| 308 |
tmpdate.day = 1; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
J = (long) (GCalendarToJD(yg, mg, dg)+2); |
| 313 |
mydate->weekday = J % 7; |
| 314 |
mydate->to_numdays = 1; /* this needs to be fixed !! */ |
| 315 |
mydate->year = tmpdate.year; |
| 316 |
mydate->month = tmpdate.month; |
| 317 |
mydate->day = tmpdate.day; |
| 318 |
|
| 319 |
/* Fill-in the structure with various nicities a user might need */ |
| 320 |
fill_datestruct(mydate, mydate->weekday, mg, mydate->month, |
| 321 |
g_day, g_day_short, g_month, g_month_short, |
| 322 |
h_day, h_day_short, h_month, h_month_short, |
| 323 |
NULL, 0); |
| 324 |
|
| 325 |
return(flag); |
| 326 |
} |
| 327 |
|
| 328 |
/****************************************************************************/ |
| 329 |
/* Name: H2G */ |
| 330 |
/* Type: Procedure */ |
| 331 |
/* Purpose: convert Hdate(year,month,day) to Gdate(year,month,day) */ |
| 332 |
/* Arguments: */ |
| 333 |
/* Input/Ouput: Hijrah date: year:yh, month:mh, day:dh */ |
| 334 |
/* Output: Gregorian date: year:yg, month:mg, day:dg , day of week:dayweek */ |
| 335 |
/* and returns flag found:1 not found:0 */ |
| 336 |
/* Note: The function will correct Hdate if day=30 and the month is 29 only */ |
| 337 |
/****************************************************************************/ |
| 338 |
int H2G(sDate *mydate, int dh, int mh, int yh) |
| 339 |
{ |
| 340 |
int found, yh1, mh1; |
| 341 |
/*int error_fill;*/ |
| 342 |
sDate tmpdate; |
| 343 |
|
| 344 |
/* make sure values are within the allowed values */ |
| 345 |
if(dh > 30) { dh = 1; mh++; } |
| 346 |
if(dh < 1) { dh = 1; mh--; } |
| 347 |
if(mh > 12) { mh = 1; yh++; } |
| 348 |
if(mh < 1) { mh = 12; yh--; } |
| 349 |
|
| 350 |
/* find the date of the begining of the month */ |
| 351 |
found = Hsub2G(mydate, mh, yh); |
| 352 |
mydate->day += dh - 1; |
| 353 |
|
| 354 |
/* Make sure that dates are within the correct values */ |
| 355 |
GDateAjust(mydate); |
| 356 |
mydate->weekday += dh - 1; |
| 357 |
mydate->weekday = mydate->weekday % 7; |
| 358 |
|
| 359 |
/*find the date of the begining of the next month*/ |
| 360 |
if(dh == 30) |
| 361 |
{ |
| 362 |
mh1 = mh + 1; |
| 363 |
yh1 = yh; |
| 364 |
if(mh1 > 12) {mh1 -= 12; yh1++;} |
| 365 |
found = Hsub2G(&tmpdate, mh1, yh1); |
| 366 |
/* Make sure that the month is 30days if not make adjustment */ |
| 367 |
if(mydate->day == tmpdate.day) |
| 368 |
{ |
| 369 |
mydate->year = tmpdate.year; |
| 370 |
mydate->month = tmpdate.month; |
| 371 |
mydate->day = 1; |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
/* Fill-in the structure with various nicities a user might need */ |
| 376 |
fill_datestruct(mydate, mydate->weekday, mh, mydate->month, |
| 377 |
h_day, h_day_short, h_month, h_month_short, |
| 378 |
g_day, g_day_short, g_month, g_month_short, |
| 379 |
NULL, 0); |
| 380 |
|
| 381 |
return(found); |
| 382 |
} |
| 383 |
|
| 384 |
/****************************************************************************/ |
| 385 |
/* Name: JDToGCalendar */ |
| 386 |
/* Type: Procedure */ |
| 387 |
/* Purpose: convert Julian Day to Gdate(year,month,day) */ |
| 388 |
/* Arguments: */ |
| 389 |
/* Input: The Julian Day: JD */ |
| 390 |
/* Output: Gregorian date: year:yy, month:mm, day:dd */ |
| 391 |
/****************************************************************************/ |
| 392 |
double JDToGCalendar(double JD, sDate *mydate) |
| 393 |
{ |
| 394 |
double A, B, F; |
| 395 |
int alpha, C, E; |
| 396 |
long D, Z; |
| 397 |
|
| 398 |
Z = (long)floor (JD + 0.5); |
| 399 |
F = (JD + 0.5) - Z; |
| 400 |
alpha = (int)((Z - 1867216.25) / 36524.25); |
| 401 |
A = Z + 1 + alpha - alpha / 4; |
| 402 |
B = A + 1524; |
| 403 |
C = (int) ((B - 122.1) / 365.25); |
| 404 |
D = (long) (365.25 * C); |
| 405 |
E = (int)(((B - D) / 30.6001)); |
| 406 |
mydate->day =(int) (B - D - floor (30.6001 * E) + F); |
| 407 |
if (E < 14) |
| 408 |
mydate->month = E - 1; |
| 409 |
else |
| 410 |
mydate->month = E - 13; |
| 411 |
if (mydate->month > 2) |
| 412 |
mydate->year = C - 4716; |
| 413 |
else |
| 414 |
mydate->year = C - 4715; |
| 415 |
|
| 416 |
F = F * 24.0; |
| 417 |
|
| 418 |
return(F); |
| 419 |
} |
| 420 |
|
| 421 |
/****************************************************************************/ |
| 422 |
/* Name: GCalendarToJD */ |
| 423 |
/* Type: Function */ |
| 424 |
/* Purpose: convert Gdate(year,month,day) to Julian Day */ |
| 425 |
/* Arguments: */ |
| 426 |
/* Input : Gregorian date: year:yy, month:mm, day:dd */ |
| 427 |
/* Output: The Julian Day: JD */ |
| 428 |
/****************************************************************************/ |
| 429 |
double GCalendarToJD(int yy, int mm, double dd) |
| 430 |
{ |
| 431 |
/* It does not take care of 1582 correction assumes correct |
| 432 |
calender from the past |
| 433 |
*/ |
| 434 |
int A, B, m, y; |
| 435 |
double T1, T2, Tr; |
| 436 |
|
| 437 |
if (mm > 2) |
| 438 |
{ |
| 439 |
y = yy; |
| 440 |
m = mm; |
| 441 |
} |
| 442 |
else |
| 443 |
{ |
| 444 |
y = yy - 1; |
| 445 |
m = mm + 12; |
| 446 |
} |
| 447 |
A = y / 100; |
| 448 |
B = 2 - A + A / 4; |
| 449 |
T1=ip (365.25 * (y + 4716)); |
| 450 |
T2=ip (30.6001 * (m + 1)); |
| 451 |
Tr=T1+ T2 + dd + B - 1524.5 ; |
| 452 |
|
| 453 |
return(Tr); |
| 454 |
} |
| 455 |
|
| 456 |
/****************************************************************************/ |
| 457 |
/* Name: GLeapYear */ |
| 458 |
/* Type: Function */ |
| 459 |
/* Purpose: Determines if Gdate(year) is leap or not */ |
| 460 |
/* Arguments: */ |
| 461 |
/* Input : Gregorian date: year */ |
| 462 |
/* Output: 0:year not leap 1:year is leap */ |
| 463 |
/****************************************************************************/ |
| 464 |
int GLeapYear(int year) |
| 465 |
{ |
| 466 |
int T = 0; |
| 467 |
|
| 468 |
if(year % 4 == 0) |
| 469 |
T = 1; /* leap_year = 1; */ |
| 470 |
|
| 471 |
if(year % 100 == 0) |
| 472 |
{ |
| 473 |
/* years=100,200,300,500,... are not leap years */ |
| 474 |
T=0; |
| 475 |
|
| 476 |
/* years=400,800,1200,1600,2000,2400 are leap years */ |
| 477 |
if(year % 400 == 0) T = 1; |
| 478 |
} |
| 479 |
|
| 480 |
return(T); |
| 481 |
} |
| 482 |
|
| 483 |
/****************************************************************************/ |
| 484 |
/* Name: GDateAjust */ |
| 485 |
/* Type: Procedure */ |
| 486 |
/* Purpose: Adjust the G Dates by making sure that the month lengths */ |
| 487 |
/* are correct if not so take the extra days to next month or year */ |
| 488 |
/* Arguments: */ |
| 489 |
/* Input: Gregorian date: year:yg, month:mg, day:dg */ |
| 490 |
/* Output: corrected Gregorian date: year:yg, month:mg, day:dg */ |
| 491 |
/****************************************************************************/ |
| 492 |
void GDateAjust(sDate *mydate) |
| 493 |
{ |
| 494 |
int dys; |
| 495 |
|
| 496 |
/* Make sure that dates are within the correct values */ |
| 497 |
/* Underflow */ |
| 498 |
if(mydate->month < 1) /* months underflow */ |
| 499 |
{ |
| 500 |
/* plus as the underflow months is negative */ |
| 501 |
mydate->month += 12; |
| 502 |
mydate->year--; |
| 503 |
} |
| 504 |
|
| 505 |
if(mydate->day < 1) /* days underflow */ |
| 506 |
{ |
| 507 |
/* month becomes the previous month */ |
| 508 |
mydate->month--; |
| 509 |
/* number of days of the month less the underflow days |
| 510 |
(it is plus as the sign of the day is negative) |
| 511 |
*/ |
| 512 |
mydate->day += gmonth[mydate->month]; |
| 513 |
if(mydate->month == 2) |
| 514 |
mydate->day += GLeapYear(mydate->year); |
| 515 |
if(mydate->month < 1) /* months underflow */ |
| 516 |
{ |
| 517 |
/* plus as the underflow months is negative */ |
| 518 |
mydate->month += 12; |
| 519 |
mydate->year--; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
/* Overflow */ |
| 524 |
if(mydate->month > 12) /* months */ |
| 525 |
{ |
| 526 |
mydate->month -= 12; |
| 527 |
mydate->year++; |
| 528 |
} |
| 529 |
|
| 530 |
if(mydate->month == 2) |
| 531 |
/* number of days in the current month */ |
| 532 |
dys = gmonth[mydate->month] + GLeapYear(mydate->year); |
| 533 |
else |
| 534 |
dys = gmonth[mydate->month]; |
| 535 |
if(mydate->day > dys) /* days overflow */ |
| 536 |
{ |
| 537 |
mydate->day -= dys; |
| 538 |
mydate->month++; |
| 539 |
if(mydate->month == 2) |
| 540 |
{ |
| 541 |
/* number of days in the current month */ |
| 542 |
dys = gmonth[mydate->month] + GLeapYear(mydate->year); |
| 543 |
if(mydate->day > dys) |
| 544 |
{ |
| 545 |
mydate->day -= dys; |
| 546 |
mydate->month++; |
| 547 |
} |
| 548 |
} |
| 549 |
if(mydate->month > 12) /* months */ |
| 550 |
{ |
| 551 |
mydate->month -= 12; |
| 552 |
mydate->year++; |
| 553 |
} |
| 554 |
} |
| 555 |
mydate->to_numdays = dys; |
| 556 |
} |
| 557 |
|
| 558 |
/****************************************************************************/ |
| 559 |
/* |
| 560 |
The day of the week is obtained as |
| 561 |
Dy=(Julian+1)%7 |
| 562 |
Dy=0 Sunday |
| 563 |
Dy=1 Monday |
| 564 |
... |
| 565 |
Dy=6 Saturday |
| 566 |
*/ |
| 567 |
/****************************************************************************/ |
| 568 |
int DayWeek(long JulianD) |
| 569 |
{ |
| 570 |
int Dy; |
| 571 |
|
| 572 |
Dy = (JulianD + 1) % 7; |
| 573 |
|
| 574 |
return(Dy); |
| 575 |
} |
| 576 |
|
| 577 |
/****************************************************************************/ |
| 578 |
/* Name: HCalendarToJD */ |
| 579 |
/* Type: Function */ |
| 580 |
/* Purpose: convert Hdate(year,month,day) to estimated Julian Day */ |
| 581 |
/* Arguments: */ |
| 582 |
/* Input : Hijrah date: year:yh, month:mh, day:dh */ |
| 583 |
/* Output: The Estimated Julian Day: JD */ |
| 584 |
/****************************************************************************/ |
| 585 |
double HCalendarToJD(int yh, int mh, int dh) |
| 586 |
{ |
| 587 |
/* Estimating The JD for hijrah dates |
| 588 |
this is an approximate JD for the given hijrah date |
| 589 |
*/ |
| 590 |
double md, yd; |
| 591 |
|
| 592 |
md = (mh - 1.0) * 29.530589; |
| 593 |
yd = (yh - 1.0) * 354.367068 + md + dh - 1.0; |
| 594 |
yd = yd + 1948439.0; /* add JD for 18/7/622 first Hijrah date */ |
| 595 |
|
| 596 |
return(yd); |
| 597 |
} |
| 598 |
|
| 599 |
/****************************************************************************/ |
| 600 |
/* Name: JDToHCalendar */ |
| 601 |
/* Type: Procedure */ |
| 602 |
/* Purpose: convert Julian Day to estimated Hdate(year,month,day) */ |
| 603 |
/* Arguments: */ |
| 604 |
/* Input: The Julian Day: JD */ |
| 605 |
/* Output : Hijrah date: year:yh, month:mh, day:dh */ |
| 606 |
/****************************************************************************/ |
| 607 |
void JDToHCalendar(double JD, sDate *mydate) |
| 608 |
{ |
| 609 |
/* Estimating the hijrah date from JD */ |
| 610 |
double md, yd; |
| 611 |
|
| 612 |
yd = JD-1948439.0; /* subtract JD for 18/7/622 first Hijrah date*/ |
| 613 |
md = mod(yd, 354.367068); |
| 614 |
mydate->day = mod(md + 0.5, 29.530589)+1; |
| 615 |
mydate->month = (int) ((md/29.530589) + 1); |
| 616 |
yd = yd - md; |
| 617 |
mydate->year = (int) (yd/354.367068 + 1); |
| 618 |
if(mydate->day > 30) {mydate->day -= 30; mydate->month++;} |
| 619 |
if(mydate->month > 12) {mydate->month -= 12; mydate->year++;} |
| 620 |
} |
| 621 |
|
| 622 |
/****************************************************************************/ |
| 623 |
/* Name: JDToHACalendar */ |
| 624 |
/* Type: Procedure */ |
| 625 |
/* Purpose: convert Julian Day to Hdate(year,month,day) */ |
| 626 |
/* Arguments: */ |
| 627 |
/* Input: The Julian Day: JD */ |
| 628 |
/* Output : Hijrah date: year:yh, month:mh, day:dh */ |
| 629 |
/****************************************************************************/ |
| 630 |
void JDToHACalendar(double JD, int *yh, int *mh, int *dh) |
| 631 |
{ |
| 632 |
int df; |
| 633 |
double HJD; |
| 634 |
sDate tmpdate; |
| 635 |
|
| 636 |
/* Estimate the Hdate that correspond to the Gdate */ |
| 637 |
JDToHCalendar(JD, &tmpdate); |
| 638 |
/* get the exact Julian Day */ |
| 639 |
HJD = HCalendarToJDA(tmpdate.year, tmpdate.month, tmpdate.day); |
| 640 |
df = (int) (JD + 0.5 - HJD); |
| 641 |
tmpdate.day += df; |
| 642 |
while(tmpdate.day > 30) |
| 643 |
{ |
| 644 |
tmpdate.day -= HMonthLength(tmpdate.year, tmpdate.month); |
| 645 |
tmpdate.month++; |
| 646 |
if(tmpdate.month > 12) |
| 647 |
{ |
| 648 |
tmpdate.year++; |
| 649 |
tmpdate.month = 1; |
| 650 |
} |
| 651 |
} |
| 652 |
if(tmpdate.day == 30 && |
| 653 |
HMonthLength(tmpdate.year, tmpdate.month) < 30) |
| 654 |
{ |
| 655 |
tmpdate.day = 1; |
| 656 |
tmpdate.month++; |
| 657 |
} |
| 658 |
if(tmpdate.month > 12) |
| 659 |
{ |
| 660 |
tmpdate.month = 1; |
| 661 |
tmpdate.year++; |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
J = JD + 2; |
| 666 |
*dayweek = J % 7; |
| 667 |
*/ |
| 668 |
*yh = tmpdate.year; |
| 669 |
*mh = tmpdate.month; |
| 670 |
*dh = tmpdate.day; |
| 671 |
} |
| 672 |
|
| 673 |
/**************************************************************************/ |
| 674 |
/* Purpose: return the integral part of a double value. */ |
| 675 |
/**************************************************************************/ |
| 676 |
double ip(double x) |
| 677 |
{ |
| 678 |
double tmp; |
| 679 |
|
| 680 |
modf(x, &tmp); |
| 681 |
|
| 682 |
return(tmp); |
| 683 |
} |
| 684 |
|
| 685 |
/**************************************************************************/ |
| 686 |
/* Name: mod */ |
| 687 |
/* Purpose: The mod operation for doubles x mod y */ |
| 688 |
/**************************************************************************/ |
| 689 |
int mod(double x, double y) |
| 690 |
{ |
| 691 |
int r; |
| 692 |
double d; |
| 693 |
|
| 694 |
d = x / y; |
| 695 |
r = (int) d; |
| 696 |
if(r < 0) |
| 697 |
r--; |
| 698 |
d = x - y * r; |
| 699 |
r = (int) d; |
| 700 |
|
| 701 |
return(r); |
| 702 |
} |
| 703 |
|
| 704 |
/**************************************************************************/ |
| 705 |
/* Purpose: returns 0 for incorrect Hijri date and 1 for correct date */ |
| 706 |
/**************************************************************************/ |
| 707 |
int IsValid(int yh, int mh, int dh) |
| 708 |
{ |
| 709 |
int valid = 1; |
| 710 |
|
| 711 |
if((yh < HStartYear) || (yh > HEndYear)) |
| 712 |
valid = 0; |
| 713 |
|
| 714 |
if( (mh < 1) || (mh > 12)) |
| 715 |
valid = 0; |
| 716 |
|
| 717 |
if( (dh < 1) || (dh > HMonthLength(yh, mh)) ) |
| 718 |
valid = 0; |
| 719 |
|
| 720 |
return(valid); |
| 721 |
} |