unit HijriLib; { ************************************************** First part of this lib which is in charge of converting to and from Hijri Calendar is reproduced form YSE Hijridate PHP Script: http://www.yse-uk.com For any possible future update or change see: http://pmc.khone.ir ************************************************** This lib is licensed under the GNU LESSER GENERAL PUBLIC LICENSE ************************************************** } interface uses jalaliLib, dialogs, SysUtils, dateutils; { type datePack = record Year : Word; Month : Word; Day : Word; end; } function gregorian_to_hijri(g_y, g_m, g_d : Word): datePack; overload; function gregorian_to_hijri(iDate : datePack): datePack; overload; function get_hmonth_name(j_m: Word) : WideString; function ConvToFarDigit(inp: Integer): WideString; overload; function ConvToFarDigit(inp: WideString): WideString; overload; procedure setHijriOffset(ofs : Integer); function getHijriOffset() : Integer; function hNow() : datePack; implementation var cnvAr : array[0..9] of WideChar = ('۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'); hDayOffset : Integer = 0; function getHijriOffset() : Integer; begin Result := hDayOffset; end; procedure setHijriOffset(ofs : Integer); begin hDayOffset := ofs; end; function ConvToFarDigit(inp: WideString): WideString; overload; var i : Word; begin for i := 1 to Length(inp) do if (Ord(inp[i]) >= 48) and (Ord(inp[i]) <= 57) then inp[i] := cnvAr[ord(inp[i]) - 48]; Result := inp; end; function ConvToFarDigit(inp: Integer): WideString; overload; begin Result := ConvToFarDigit(IntToStr(inp)); end; function greg2jd(g_y, g_m, g_d : Integer) : Extended; begin Result := (1461 * (g_y + 4800 + (g_m - 14) / 12)) / 4 + (367 * (g_m - 2 - 12 * ((g_m - 14) / 12))) / 12 - (3 * ((g_y + 4900 + (g_m - 14) / 12) / 100 )) / 4 + g_d - 32075; end; function jd2hijri(jd : Extended) : datePack; var h_n, h_j : Integer; begin jd := jd - 1948440 + 10632; h_n := Trunc((jd - 1) / 10631); jd := jd - 10631 * h_n + 354; h_j := (Trunc ((10985 - jd) / 5316)) * (Trunc (50 * jd / 17719)) + (Trunc (jd / 5670)) * (Trunc (43 * jd / 15238)); jd := jd - (Trunc ((30 - h_j) / 15)) * (Trunc ((17719 * h_j) / 50)) - (Trunc (h_j / 16)) * (Trunc ((15238 * h_j) / 43)) + 29; Result.Month := Trunc(24 * jd / 709); Result.Day := Round(jd - Trunc(709 * Result.Month / 24)) + hDayOffset; Result.Year := 30 * h_n + h_j - 30; end; function gregorian_to_hijri(g_y, g_m, g_d : Word): datePack; overload; begin Result := jd2hijri(greg2jd(g_y, g_m, g_d)); end; function gregorian_to_hijri(iDate : datePack): datePack; overload; begin Result := gregorian_to_hijri(iDate.Year, iDate.Month, iDate.Day); end; function get_hmonth_name(j_m: Word) : WideString; begin case j_m of 1 : Result := 'محرّم'; 2 : Result := 'صفر'; 3 : Result := 'ربيع الأول'; 4 : Result := 'ربيع الآخر'; 5 : Result := 'جمادى الأول'; 6 : Result := 'جمادى الآخر'; 7 : Result := 'رجب'; 8 : Result := 'شعبان'; 9 : Result := 'رمضان'; 10 : Result := 'شوّال'; 11 : Result := 'ذو القعدة'; 12 : Result := 'ذو الحجة'; end; end; function hNow() : datePack; begin Result := gregorian_to_hijri(YearOf(Now), MonthOf(Now), DayOf(Now)); end; end.