Search code examples
gogregorian-calendarhijri

Hijiri date to Gregorian date in golang


I'm trying to convert a Hijri Date to Gregorian date according to Umm AlQura calendar. I couldn't find much on the Internet (maybe attributing to my lazy attitude). I tried converting the Hijri date to Julian number and then from Julian number to Gregorian date.

However, I just could convert Hijri date to Julian number. I cannot figure out how to convert from Julian number to Gregorian.

Or maybe I'm taking the wrong approach. Can anyone please help to convert a Hijri date according to Umm AlQura calendar to a Gregorian date in golang?

This is what I've so far:

func getJulianNumber(y, m, d int) float64 {
    if m == 1 || m == 2 {
        y -= 1
        m += 12
    }
    a := y / 100
    b := a / 4
    c := 2 - a + b
    e := int(365.25 * (float64(y) + 4716))
    f := int(30.6001 * (float64(m) + 1))
    return float64(float64(c) + float64(d) + float64(e) + float64(f) - 1524.5)
}

func julianToH(jd float64) {
    q := jd + 0.5
    z := int(q)
    w := int((float64(z) - 1867216.25) / 36524.25)
    x := w / 4
    a := int(float64(z) + 1 + float64(w) - float64(x))
    b := a + 1524
    c := int((float64(b) - 122.1) / 365.25)
    d := int(365.25 * float64(c))
    e := int((float64(b) - float64(d)) / 30.6001)
    f := int(30.6001 * float64(e))

    day := int(float64(b) - float64(d) - float64(f) + (q - float64(z)))
    var month, year int
    e1 := e - 1
    if e1 <= 12 {
        month = e1
    } else {
        month = e - 3
    }
    if month == 1 || month == 2 {
        year = int(c - 4715)
    } else {
        year = int(c - 4716)
    }
    fmt.Println(day, month, year)
}

Much obliged!


Solution

  • I created a small helper library hijri to solve my problem. Thanks for all the help!