import mx.DateTime

def getNextBoozeupDate(relativeDate=None):
    """Return the date of the next Python South-East UK booze up as an
    mx.DateTime.DateTime object.

    Optional parameter relativeDate defaults to current date."""
    
    relativeDate = relativeDate or mx.DateTime.today()
    
    thisMonthsBoozeupDate = getMonthsBoozeupDate(year=relativeDate.year, month=relativeDate.month)
    if thisMonthsBoozeupDate >= relativeDate:
        return thisMonthsBoozeupDate

    nextMonthsBoozeupDate = getMonthsBoozeupDate(year=relativeDate.year, month=((relativeDate.month + 1) % 12))
    return nextMonthsBoozeupDate

def getMonthsBoozeupDate(year=0, month=0):
    """Return the date of the Python South-East UK booze up in a given month
    as an mx.DateTime.DateTime object.

    The booze up will be on the 2nd Thursday of the month, unless the 1st
    Thursday is the 1st day of the month, in which case the booze up will be
    on the 1st.

    Optional parameters year and month default to the current date."""
    
    today = mx.DateTime.today()
    year = year or today.year
    month = month or today.month

    boozeupDate = mx.DateTime.DateTimeFrom(year=year, month=month) \
                + mx.DateTime.RelativeDateTime(weekday=(mx.DateTime.Thursday, 2))
    
    if boozeupDate.day == 8:
        boozeupDate = boozeupDate - 7

    return boozeupDate        
        
if __name__ == '__main__':
    print getNextBoozeupDate()