PL/SQL function that converts Gregorian dates to Hijri
FUNCTION Georgian2Hijri(G_DATE Date) RETURN Varchar2 IS
H_DAY NUMBER(2):=0;
HDAY VARCHAR2(2);
H_MONTH NUMBER(2):=1;
HMONTH VARCHAR2(2);
H_YEAR NUMBER(4):=1;
FGDATE DATE;
I_LOOP NUMBER;
HY NUMBER;
MONTH_NAME VARCHAR2(40);
BEGIN
FGDATE := TO_DATE('14-07-0622','DD-MM-YYYY');
I_LOOP := G_DATE - FGDATE;
LOOP
IF I_LOOP<355 THEN
EXIT;
END IF;
HY := TRUNC( H_YEAR / 30 ) * 30;
IF (H_YEAR - HY) IN (2,5,7,10,13,16,18,21,24,26,29) THEN
H_YEAR := H_YEAR + 1;
I_LOOP := I_LOOP - 355;
ELSE
I_LOOP := I_LOOP - 354;
H_YEAR := H_YEAR + 1;
END IF;
END LOOP;
FOR I IN 1..I_LOOP LOOP
H_DAY := H_DAY + 1;
IF (H_MONTH=1 OR H_MONTH=3 OR H_MONTH=5 OR H_MONTH=7 OR H_MONTH=9 OR H_MONTH=11) AND H_DAY>30 THEN
H_MONTH := H_MONTH + 1;
H_DAY := 1;
END IF;
IF (H_MONTH=2 OR H_MONTH=4 OR H_MONTH=6 OR H_MONTH=8 OR H_MONTH=10)AND H_DAY>29 THEN
H_MONTH := H_MONTH + 1;
H_DAY := 1;
END IF;
HY := TRUNC( H_YEAR / 30 ) * 30;
IF (H_YEAR - HY) IN (2,5,7,10,13,16,18,21,24,26,29) THEN
IF H_MONTH=12 AND H_DAY>30 THEN
H_MONTH := H_MONTH + 1;
H_DAY := 1;
IF H_MONTH>12 THEN
H_YEAR := H_YEAR + 1;
H_MONTH := 1;
END IF;
END IF;
ELSE
IF H_MONTH=12 AND H_DAY>29 THEN
H_MONTH := H_MONTH + 1;
H_DAY := 1;
IF H_MONTH>12 THEN
H_YEAR := H_YEAR + 1;
H_MONTH := 1;
END IF;
END IF;
END IF;
END LOOP;
SELECT DECODE(H_MONTH
,1,'محرم'
,2,'صفر'
,3,'ربيع الأول'
,4,'ربيع الثاني
,5,'جمادى الأولى
,6,'جمادة الآخرة
,7,'رجب'
,8,'شعبان'
,9,'رمضان'
,10,'شوال'
,11,'ذي القعدة'
,12,ذي الحجة',NULL)
INTO MONTH_NAME
FROM DUAL;
IF H_DAY<10 THEN
HDAY := '0' || TO_CHAR(H_DAY);
ELSE
HDAY := TO_CHAR(H_DAY);
END IF;
IF H_MONTH<10 THEN
HMONTH := '0' || TO_CHAR(H_MONTH);
ELSE
HMONTH := TO_CHAR(H_MONTH);
END IF;
Return (HDAY || '-' || MONTH_NAME || '-' || TO_CHAR(H_YEAR));
END Georgian2Hijri;
| 0 | |
|---|---|
| 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 | |
Key Components
1. Input/Output
- Takes Gregorian date (`G_DATE`)
- Returns Hijri date as string (e.g., 01-ربيع الأول-1446)
2. Base Reference
Uses the Hijri epoch: 14 July 622 AD (`FGDATE`)
3. Variables
- `H_DAY/H_MONTH/H_YEAR`: Track Hijri date components
- `I_LOOP`: Days between input date and epoch
- `HY`: Leap year calculation helper
Conversion Process
1. Year Calculation
- Subtracts epoch date from input date to get total days (`I_LOOP`)
- Uses 30-year lunar cycles (11 leap years per cycle):
- Regular years = 354 days
- Leap years = 355 days
- Adjusts year count based on remaining days
2. Day/Month Calculation
- Processes remaining days using Hijri month rules:
- Odd months = 30 days (Muharram, Safar, etc.)
- Even months = 29 days
- Dhu al-Hijjah (12th month) varies:
- 30 days in leap years
- 29 days otherwise
3. Month Names
Uses Arabic month names via `DECODE`:
1 → محرم (Muharram)
3 → ريع الأول (Rabi' al-Awwal)
9 → رمضان (Ramadan)
12 → ذو الحجة (Dhu al-Hijjah)
4. Formatting
- Adds leading zeros to single-digit days/months
- Returns final string in DD-MonthName-YYYY format
Key Characteristics
- Algorithm Type: Approximate astronomical calculation
- Accuracy: Follows tabular Islamic calendar rules
- Limitations:
- Doesn't account for lunar sightings
- Uses fixed leap year pattern
- Month names require Unicode support
This implementation provides a practical conversion method for applications needing approximate Hijri dates without real-time moon observations.
1. Input/Output
- Takes Gregorian date (`G_DATE`)
- Returns Hijri date as string (e.g., 01-ربيع الأول-1446)
2. Base Reference
Uses the Hijri epoch: 14 July 622 AD (`FGDATE`)
3. Variables
- `H_DAY/H_MONTH/H_YEAR`: Track Hijri date components
- `I_LOOP`: Days between input date and epoch
- `HY`: Leap year calculation helper
Conversion Process
1. Year Calculation
- Subtracts epoch date from input date to get total days (`I_LOOP`)
- Uses 30-year lunar cycles (11 leap years per cycle):
- Regular years = 354 days
- Leap years = 355 days
- Adjusts year count based on remaining days
2. Day/Month Calculation
- Processes remaining days using Hijri month rules:
- Odd months = 30 days (Muharram, Safar, etc.)
- Even months = 29 days
- Dhu al-Hijjah (12th month) varies:
- 30 days in leap years
- 29 days otherwise
3. Month Names
Uses Arabic month names via `DECODE`:
1 → محرم (Muharram)
3 → ريع الأول (Rabi' al-Awwal)
9 → رمضان (Ramadan)
12 → ذو الحجة (Dhu al-Hijjah)
4. Formatting
- Adds leading zeros to single-digit days/months
- Returns final string in DD-MonthName-YYYY format
Key Characteristics
- Algorithm Type: Approximate astronomical calculation
- Accuracy: Follows tabular Islamic calendar rules
- Limitations:
- Doesn't account for lunar sightings
- Uses fixed leap year pattern
- Month names require Unicode support
This implementation provides a practical conversion method for applications needing approximate Hijri dates without real-time moon observations.