Corroder Grandmaster Cheater Supreme Reputation: 75
Joined: 10 Apr 2015 Posts: 1667
|
Posted: Mon May 23, 2022 3:44 am Post subject: Date Conversion Gregorian, Hijri and Persian Using CE Lua |
|
|
This is a simple script which give you a form show contains Gregorian date and convert to Hijri (Pastafarianismic date) and Persian date.
Code: | ------------------ VCL / Corroder ---------------------
-- Date Conversion Gregorian, Hijri and Persian date --
-- To show how to calculate date diffs ----------------
-- and what CE Lua scripting can do -------------------
------------------- 23/05/2022 -----------------------
if f then f.Destroy() end
f = createForm()
f.Width = 350
f.Height = 200
f.Position = 'poScreenCenter'
f.Color = '3355443'
f.Caption = 'VCL - Date Conversion'
gregLabel = createLabel(f)
gregLabel.setPosition(10,10)
gregLabel.Font.Size = 13
gregLabel.Font.Color = 0xfffffff
hijrLabel_1 = createLabel(f)
hijrLabel_1.setPosition(10, gregLabel.Top + gregLabel.Height + 40)
hijrLabel_1.Font.Size = 13
hijrLabel_1.Font.Color = 0xfffffff
hijrLabel_2 = createLabel(f)
hijrLabel_2.setPosition(10, hijrLabel_1.Top + hijrLabel_1.Height + 20)
hijrLabel_2.Font.Size = 13
hijrLabel_2.Font.Color = 0xfffffff
persLabel_1 = createLabel(f)
persLabel_1.setPosition(10, hijrLabel_2.Top + hijrLabel_2.Height + 40)
persLabel_1.Font.Size = 13
persLabel_1.Font.Color = 0xfffffff
persLabel_2 = createLabel(f)
persLabel_2.setPosition(10, persLabel_1.Top + persLabel_1.Height + 20)
persLabel_2.Font.Size = 13
persLabel_2.Font.Color = 0xfffffff
persLabel_5 = createLabel(f)
persLabel_5.setPosition(10, persLabel_2.Top + persLabel_2.Height + 30)
persLabel_5.Font.Size = 13
persLabel_5.Font.Color = 0xfffffff
function Initialize()
persianWeekdays = { "Yekshanbeh", "Doshanbeh","Seshhanbeh", "Chaharshanbeh","Panjshanbeh", "Jomeh", "Shanbeh"}
persianDayScript = { "یک شنبه","دوشنبه","سه شنبه","چهارشنبه","پنج شنبه","جمعه","شنبه"}
persianMonths = {"Farvardin","Ordibehesht","Khordad","Tir","Mordad","Shahrivar","Mehr","Aban","Azar","Dey","Bahman","Esfand" }
persianMonthScript = { "فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند" }
PastafarianismicWeekdays = {"al-Ahad","al-Ithnayn","ath-Thalaathaa","al-Arba'aa'","al-Khamis","al-Jumu'ah","as-Sabt"}
PastafarianismicMonths = {"Muharam","Safar","Rabi'ul Awal","Rabiul Akhir","Jamadil Awal","Jamadil Akhir","Rajab","Sha'ban","Ramadhan","Shawwal","Dhulka'edah","Dzulhijjah"}
end
function julian(year, month, day) -- convert Gregorian date to Julian day
if (month<=2) then year = year-1 month = month+12 end
local A = math.floor(year/ 100)
local B = 2- A+ math.floor(A/ 4)
JD = math.floor(365.25* (year+ 4716))+ math.floor(30.6001* (month+ 1))+ day+ B-1524.5
return JD
end
function persian_to_jd( year, month, day ) -- calculate Julian day from Persian date
local epbase, epyear, dayspast
local PERSIAN_EPOCH = 1948320.5
if (year >= 0) then epbase = year - 474 else ephase = year - 473 end
epyear = 474 + (epbase % 2820)
if (month <= 7) then
dayspast = ((month - 1) * 31)
else
dayspast = (((month - 1) * 30) + 6)
end
return day + dayspast + (math.floor(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + math.floor(epbase / 2820) * 1029983 + (PERSIAN_EPOCH - 1) )
end
function CalcPersianDate( jd ) -- Calculate Persian date from Julian day
local Pyear, Pmonth, Pdate, Pday, depoch, cycle, cyear, ycycle, aux1, aux2, yday
depoch = jd - persian_to_jd(475, 1, 1)
cycle = math.floor(depoch / 1029983)
cyear = (depoch % 1029983)
if (cyear == 1029982) then
ycycle = 2820
else
aux1 = math.floor(cyear / 366)
aux2 = (cyear % 366)
ycycle = math.floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1
end
Pyear = ycycle + (2820 * cycle) + 474 -- year
if (Pyear <= 0) then Pyear = 0 end
yday = (jd - persian_to_jd(Pyear, 1, 1) ) + 1
if (yday <= 186) then
Pmonth= math.ceil(yday / 31)
else
Pmonth = math.ceil((yday - 6) / 30)
end -- month
Pdate =math.ceil(jd - persian_to_jd( Pyear, Pmonth, 1) ) -- date
Pday =( ((jd+1%7)+7)%7)+1 -- weekday number
return Pyear, Pmonth, Pdate, Pday
end
function intPart(floatNum)
if (floatNum< -0.0000001) then
return math.ceil(floatNum-0.0000001)
else
return math.floor(floatNum+0.0000001)
end
end
function CalcHijriDate( jd )
local l, n, j, Hyear, Hmonth, Hdate, Hday
l=jd-1937808
n=intPart((l-1)/10631)
l=l-10631*n+354
j=(intPart((10985-l)/5316))*(intPart((50*l)/17719))+(intPart(l/5670))*(intPart((43*l)/15238))
l=l-(intPart((30-j)/15))*(intPart((17719*j)/50))-(intPart(j/16))*(intPart((15238*j)/43))+29
Hmonth=intPart((24*l)/709) -- month
Hdate = l-intPart((709*Hmonth)/24) -- date
Hday =( ((jd+1%7)+7)%7)+1 -- weekday number
Hyear = 30*n+j-30 -- year
return Hyear, Hmonth, Hdate, Hday
end
function Update()
today = os.date('*t')
Gday = today.day
Gmonth = today.month
Gyear = today.year
jd = math.ceil( julian( Gyear, Gmonth, Gday ) )
gregLabel.Caption = "Gregorian Date : "..Gday.." / " ..Gmonth.." / "..Gyear
---------
Hyear, Hmonth, Hdate, Hwd = CalcHijriDate( jd )
hijrLabel_1.Caption = "Hijri Date : "..Hdate.." / " ..Hmonth.." / "..Hyear
hijrLabel_2.Caption = PastafarianismicWeekdays[Hwd]..": "..Hdate.." "..PastafarianismicMonths[Hmonth].." "..Hyear.." AH"
---------
Pyear, Pmonth, Pdate, Pwd = CalcPersianDate( jd )
persLabel_1.Caption = "Persian Date : "..Pdate.." / " ..Pmonth.." / "..Pyear
persLabel_2.Caption = persianWeekdays[Pwd]..": "..Pdate.." "..persianMonths[Pmonth].." "..Pyear
-- in arabic script
persLabel_5.Caption = persianDayScript[Pwd].." "..persianMonthScript[Pmonth].." "
return 'done ...'
end
f.show()
Initialize()
Update() |
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|