[DB-SIG] Python: convert Hijri to Gregorian Date and vice versa

M.-A. Lemburg mal at egenix.com
Mon Nov 24 21:01:03 CET 2014


Hello Lucie,

this list is about relational databases, not date/time questions.

You may get help on comp.lang.python or the Python tutor list:

https://mail.python.org/mailman/listinfo/tutor

BTW: The Hirji calendar is somewhat difficult to map to an algorithm,
due to its reliance on (official) sightings of the moon. You are probably
looking for a variant of the tabular Islamic calendar:

http://en.wikipedia.org/wiki/Islamic_calendar

For some interesting research on the subject, see:

http://www.math.nus.edu.sg/aslaksen/calendar/islamic.html

Thanks,
-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Nov 24 2014)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2014-11-19: Released mxODBC Connect 2.1.1 ...     http://egenix.com/go65
2014-11-11: Released eGenix pyOpenSSL 0.13.6 ...  http://egenix.com/go64

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/

On 20.11.2014 10:52, Lucie Krennwallner wrote:
> Dear Mr Egli,
> 
> online I found your Python code for Islamic & Gregorian Dates
> (https://mail.python.org/pipermail/db-sig/1997-December/000361.html) and
> wondered whether you might help me:
> 
> I have python script (see below) that to translate Hijri Dates to Gregorian
> Date and vice versa. But the days might be off for about 1 day.
> eg
> gregorian date: 31/01/2013 is translated into
> Hirji date: 19-03-1434
> instead of the correct 20-03-2013.
> 
> My guess: there is a problem with leap years and last days of month and/or
> leap years.
> Python knowledge is not good enough to correct the code implement your
> suggestions.
> Would you kindly help me? Any tiny advice will be helpful!
> What do I have to change (add) to get the correct result?
> 
> Thanks for any effort!
> 
> All the best,
> Lucie Krennwallner
> 
> 
> Python Script modified from
> http://gersham-xbmc-repo.googlecode.com/svn/trunk/addons/script.islamic.prayer.times/resources/lib/Hijri.py
> 
> 
> 1) GREGORIAN DATE CONVERTED TO HIJRI DATES
> 
> import math
> 
> def intPart(floatNum):
>     if floatNum < -0.0000001: return math.ceil(floatNum - 0.0000001)
>     return math.floor(floatNum + 0.0000001)
> 
> 
> def Gregorian2Hijri(yr, mth, day):
> 
>         jd1 = intPart((1461 * (yr + 4800 + intPart((mth - 14) / 12.0))) / 4)
>         jd2 = intPart((367 * (mth - 2 - 12 *  (intPart((mth - 14) /
> 12.0)))) / 12)
>         jd3 = intPart((3 * (intPart((yr + 4900 + intPart((mth - 14) /
> 12.0)) / 100))) / 4)
>         jd = jd1 + jd2 - jd3 + day - 32075
> 
>         l = jd - 1948440 + 10632
>         n = intPart((l - 1) /10631.0)
>         l = l - 10631 * n + 354
> 
>         j1 = (intPart((10985 - l) / 5316.0)) * (intPart((50 * l) / 17719.0))
>         j2 = (intPart(l / 5670.0)) * (intPart((43 * l) / 15238.0))
>         j = j1 + j2
> 
>         l1 = (intPart((30 - j) / 15.0)) * (intPart((17719 * j) / 50.0))
>         l2 = (intPart(j / 16.0)) * (intPart((15238 * j) / 43.0))
>         l = l - l1 - l2 + 29
> 
> 
>         m = intPart((24 * l) / 709.0)
>         y = 30 * n + j - 30
>         d = l - intPart((709 * m) / 24.0)
> 
> 
> 
> yr = int(raw_input("Gegorian year: "))
> mth= int(raw_input("Gegorian month: "))
> day = int(raw_input("Gegorian day: "))
> hi = Gregorian2Hijri(yr, mth, day)
> 
> print hi
> 
> 2) HIJRI DATE CONVERTED TO GREGORIAN DATES
> 
> import math
> 
> def intPart(floatNum):
>     if floatNum < -0.0000001: return math.ceil(floatNum - 0.0000001)
>     return math.floor(floatNum + 0.0000001)
> 
> def Hijri2Gregorian(yr, mth, day):
>     jd1 = intPart((11 * yr + 3) / 30.0)
>     jd2 = intPart((mth - 1) / 2.0)
>     jd = jd1 + 354 * yr + 30 * mth - jd2 + day + 1948440 - 385
> 
> 
>     l = jd + 68569
>     n = intPart((4 * l) / 146097.0)
>     l = l - intPart((146097 * n + 3) / 4.0)
>     i = intPart((4000 * (l + 1)) / 1461001.0)
>     l = l - intPart((1461 * i) / 4.0) + 31
>     j = intPart((80 * l) / 2447.0)
>     d = l - intPart((2447 * j) / 80.0)
>     l = intPart(j / 11.0)
>     m = j + 2 - 12 * l
>     y = 100 * (n - 49) + i + l
>     return y, m, d
> 
> yr = int(raw_input("Provide year, like 1395: "))
> mth = int(raw_input("Provide year, like 07: "))
> day = int(raw_input("Provide year, like 12: "))
> 
> inp =Hijri2Gregorian(yr, mth, day)
> print inp
> 
> 
>  Lucie Krennwallner | BI Consultant
> t: +44 (0)1202 373333 | m: +49 (0)176 328 324 62
> We're Hiring! <http://interworks.co.uk/careers/> | Support
> <http://support.interworks.co.uk/> | Blog <http://interworks.co.uk/blog/> |
> LinkedIn <http://www.linkedin.com/company/interworks-uk> | Facebook
> <https://www.facebook.com/interworks>[image: InterWorks Europe]
> <http://interworks.co.uk/>Unit 1, Christchurch Business Park, Dorset, BH23
> 4FLCo Reg No 08368863 | VAT 154881290
> 
> 
> 
> _______________________________________________
> DB-SIG maillist  -  DB-SIG at python.org
> https://mail.python.org/mailman/listinfo/db-sig
> 




More information about the DB-SIG mailing list