def GetPrevConjunction(date, earth, forward=False):
    prevSun = 0.0
    prevMoon = 0.0
    prevDiff = 0.0
    nowSun = 0.0
    nowMoon = 0.0
    nowDiff = 0.0
    dir = 1.0 if forward else -1.0
    moon = GCMoonData.MOONDATA()

    d = GCGregorianDate(date=date)
    d.shour = 0.5
    d.tzone = 0.0
    jd = d.GetJulian()

    # set initial data for input day
    # NOTE: for grenwich
    moon.Calculate(jd, earth)
    prevSun = GCSunData.GetSunLongitude(d)
    prevMoon = moon.longitude_deg
    prevDiff = GCMath.putIn180(prevSun - prevMoon)

    for bCont in range(32):
        if forward:
            d.NextDay()
        else:
            d.PreviousDay()
        jd += dir
        moon.Calculate(jd, earth)
        nowSun = GCSunData.GetSunLongitude(d)
        nowMoon = moon.longitude_deg
        nowDiff = GCMath.putIn180(nowSun - nowMoon)

        if IsConjunction(nowMoon, nowSun, prevSun, prevMoon):
            # now it calculates actual time and zodiac of conjunction
            if prevDiff == nowDiff: return 0
            x = math.fabs(nowDiff) / math.fabs(prevDiff - nowDiff)
            if x < 0.5:
                if forward: d.PreviousDay()
                d.shour = x + 0.5
            else:
                if not forward: d.NextDay()
                d.shour = x - 0.5
            date.Set(d)
            prevSun = GCMath.putIn360(prevSun)
            nowSun = GCMath.putIn360(nowSun)
            if math.fabs(prevSun - nowSun) > 10.0:
                return GCMath.putIn180(nowSun) + (GCMath.putIn180(prevSun) -
                                                  GCMath.putIn180(nowSun)) * x
            else:
                return nowSun + (prevSun - nowSun) * x
        prevSun = nowSun
        prevMoon = nowMoon
        prevDiff = nowDiff

    return 1000.0
def IsConjunction(m1, s1, s2, m2):
    if m2 < m1: m2 += 360.0
    if s2 < s1: s2 += 360.0
    if (m1 <= s1) and (s1 < s2) and (s2 <= m2):
        return True

    m1 = GCMath.putIn180(m1)
    m2 = GCMath.putIn180(m2)
    s1 = GCMath.putIn180(s1)
    s2 = GCMath.putIn180(s2)

    return (m1 <= s1) and (s1 < s2) and (s2 <= m2)
def GetPrevConjunctionEx(test_date, found, this_conj, earth, forward=False):
    phi = 12.0
    dir = 1.0 if forward else -1.0
    if this_conj:
        test_date.shour += 0.2 * dir
        test_date.NormalizeHours()

    jday = test_date.GetJulianComplete()
    moon = GCMoonData.MOONDATA()
    d = GCGregorianDate(date=test_date)
    xd = GCGregorianDate()
    scan_step = 1.0
    prev_tit = 0
    new_tit = -1

    moon.Calculate(jday, earth)
    sunl = GCSunData.GetSunLongitude(d)
    l1 = GCMath.putIn180(moon.longitude_deg - sunl)
    prev_tit = int(math.floor(l1 / phi))

    counter = 0
    while counter < 20:
        xj = jday
        xd.Set(d)

        jday += scan_step * dir
        d.shour += scan_step * dir
        d.NormalizeHours()

        moon.Calculate(jday, earth)
        sunl = GCSunData.GetSunLongitude(d)
        l2 = GCMath.putIn180(moon.longitude_deg - sunl)
        new_tit = int(math.floor(l2 / phi))

        if forward:
            is_change = prev_tit < 0 and new_tit >= 0
        else:
            is_change = prev_tit >= 0 and new_tit < 0

        if is_change:
            jday = xj
            d.Set(xd)
            scan_step *= 0.5
            counter += 1
        else:
            l1 = l2
            prev_tit = new_tit

    found.Set(d)
    return sunl