from DateTime import DateTime

def seqToDate(dateSeq):
    """
    Generate an ISO date string from dateSeq.  dateSeq is a sequence of up
    to three integer strings.  dateSeq is not interpreted the same way that
    DateTime does - the sequence is always used as (year, month, day), with
    the max value substituted for missing values.
    """
    if len(dateSeq) > 3:
        raise
    if dateSeq:
        year =  dateSeq.pop(0)
    else:
        # no dateSeq, just return latest
        return DateTime().latestTime().ISO()
    if dateSeq:
        month = dateSeq.pop(0)
    else:
        month = '12'
    if dateSeq:
        day = dateSeq.pop(0)
    else:
        if month in ('1','3','5','7','8','10','12'):
            day = '31'
        elif month in ('4','6','9','11'):
            day = '30'
        elif DateTime('%s/1/1' % year).isLeapYear():
            day = '28'
        else:
            day = '29'
    return DateTime('%s/%s/%s' % (year, month, day)).latestTime().ISO()

def dateToSeq(date, resolution):
    """
    Generate sequence of year, month, day, etc. int strings given a
    DateTime.   Resolution describes how many
    elements in the sequence, with a maximum of 6.
    """
    return map(str, map(int, date.parts()[0:min(resolution, 6)]))
