Parse the --date value and return a couple of datetime object.
    The format is [YYYY]MMDD[,[YYYY]MMDD].

def parse_date_period(date):
    """
    Parse the --date value and return a couple of datetime object.
    The format is [YYYY]MMDD[,[YYYY]MMDD].
    """
    import datetime
    
    now = datetime.datetime.today()
    date_len = len(date)
    
    if date_len == 4:
        date1 = str(now.year) + date
        date2 = str(now.year) + date + "235959"
    elif date_len == 8:
        date1 = date
        date2 = date + "235959"
    elif date_len == 9:
        if date[4] != ',':
            raise TypeError
        date1 = str(now.year) + date[0:4]
        date2 = str(now.year) + date[5:9] + "235959"
    elif date_len == 17:
        if date[8] != ',':
            raise TypeError
        date1 = date[0:8]
        date2 = date[9:17] + "235959"
    else:
        raise TypeError

    try:
        date1 = datetime.datetime.strptime(date1, "%Y%m%d")
    except ValueError:
        if date_len < 9:
            raise ValueError("Error of date value in --date parameter, use --date=[YYYY]MMDD")
        else:
            raise ValueError("Error in the first date value in --date parameter, "
                             "use --date=[YYYY]MMDD,[YYYY]MMDD")
    try:
        date2 = datetime.datetime.strptime(date2, "%Y%m%d%H%M%S")        
    except ValueError:
        raise ValueError("Error in the second date value in --date parameter, "
                         "use --date=[YYYY]MMDD,[YYYY]MMDD")
        
    if date1 > date2:
        raise ValueError("Wrong parameter --date: the first date is after the second!")

    return date1, date2