Seemingly strange behavior with python counter program
Question: A portion of a recent assignment was to design a program that counts days
between dates. I have yet to finish, and I know this could be improved
drastically. However, my question is: when I run this program with **date2**
(below), an error occurs, but this runs as planned with **date1** (again,
below). I find this behavior strange. These dates are only one day apart and
yet one fails and one doesn't. I've tried this with multiple dates, but each
fails after 994 days regardless of month, year, day, whatever. **Why?**

clarification: I define "fail" as

    
    
      File "first.py", line 35, in counter
        return counter(date_array, x+1)
    

.

    
    
    def date_test(date_array):
        first = (date_array[0], date_array[1], date_array[2])
        second = (date_array[3], date_array[4], date_array[5])
        if first > second:
            return False
        elif first != second:
            return True
        elif first == second:
            return False
        else:
            return "Error: This shouldn't happen."
    
    def counter(date_array, x = 0):
        months = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        while(date_test(date_array)):
            if(date_array[2] == 31 and date_array[1] == 12):
                date_array[0] += 1; date_array[1], date_array[2] = 1, 1
                return counter(date_array, x+1)
            else:
                if date_array[2] < months[date_array[1]-1]:
                    date_array[2] += 1
                    return counter(date_array, x+1)
                else:
                    date_array[1] += 1; date_array[2] = 1;
                    return counter(date_array, x+1);
        return x
    
    def tuple_test(date):
        date_array = []
        for x in date:
            date_array.append(x)
        if not date_test(date_array):
           return "The first date is before the second. Swap them."
        return counter(date_array)
    
    date1 = (1900,1,1,1902,9,22)
    date2 = (1900,1,1,1902,9,23)
    
    print tuple_test(date1)
    print tuple_test(date2)
    


Answer: you should use the tools python provides

    
    
    >>> import datetime
    >>> date1 = (1900,1,1,1902,9,22) #your weird list with 2 dates
    >>> dt1 = datetime.date(*date1[:3]) #create a date object  == datetime.date(1900,1,1)
    >>> dt2 = datetime.date(*date1[3:]) #create a date object  == datetime.date(1902,9,22)
    >>> if dt1 < dt2: dt1,dt2 = dt2,dt1 #if dt1 is smaller than dt2, swap them
    ...
    >>> print (dt1 - dt2).days #subtract them and print their days
    994
    >>>
    

