Last active
October 4, 2024 09:39
-
-
Save mrunderline/640e25e6bb72d8a829d9ff3701876fe6 to your computer and use it in GitHub Desktop.
a postgres function that convert Gregorian date to Jalali
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE OR REPLACE FUNCTION g2j(in_date timestamp with time zone) | |
RETURNS character varying AS | |
$BODY$ | |
DECLARE | |
aday smallint; | |
amonth smallint; | |
ayear smallint; | |
a1 char(4); | |
b1 char(2); | |
c1 char(2); | |
Tday smallint; | |
Tmonth smallint; | |
Tyear smallint; | |
CabisehYear smallint; | |
TMonthEnd smallint; | |
numdays int; | |
Const_Date timestamp without time zone; | |
BEGIN | |
Const_Date = cast('3/21/1921' as timestamp without time zone); | |
numdays = DATE_PART('day', in_date - Const_Date); | |
aday = 1; | |
amonth = 1; | |
ayear = 1300; | |
CabisehYear = cast((numdays / 1461) as int); | |
numdays = numdays - CabisehYear * 1461; | |
Tyear = cast((numdays / 365) as int); | |
If Tyear = 4 | |
then | |
Tyear = Tyear - 1; | |
end if; | |
numdays = numdays - Tyear * 365; | |
Tmonth = cast((numdays / 31) as int); | |
If (Tmonth > 6) | |
then | |
Tmonth = 6; | |
end if; | |
numdays = numdays - Tmonth * 31; | |
TMonthEnd = 0; | |
If (numdays >= 30 And Tmonth = 6) | |
then | |
TMonthEnd = cast((numdays / 30) as int); | |
If TMonthEnd >= 5 | |
then | |
TMonthEnd = 5; | |
end if; | |
numdays = numdays - TMonthEnd * 30; | |
End if; | |
Tmonth = (TMonthEnd + Tmonth); | |
Tday = numdays; | |
Tyear = (Tyear + CabisehYear * 4); | |
ayear = (ayear + Tyear); | |
amonth = amonth + Tmonth; | |
aday = aday + Tday; | |
a1 = ayear; | |
b1 = amonth; | |
c1 = aday; | |
If length(b1) = 1 | |
then | |
b1 = '0' || b1; | |
end if; | |
If length(c1) = 1 | |
then | |
c1 = '0' || c1; | |
end if; | |
return a1 || '-' || b1 || '-' || c1; | |
END; | |
$BODY$ | |
LANGUAGE plpgsql | |
VOLATILE | |
COST 100; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment