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

Lucie Krennwallner lucie at interworks.eu
Thu Nov 20 10:52:52 CET 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/db-sig/attachments/20141120/ed3ce85a/attachment.html>


More information about the DB-SIG mailing list