validDate Revisited – Python

  1. Zeller Pascal
  2. Zeller Java
  3. validDate Revisited – Python
  4. validDate Revisited – Java
  5. Java GUI
  6. GUI – Refinements – ComboBoxes
  7. GUI – Refinements – Error Messages
  8. GUI Refinements – Focus
  9. GUI Refinements – Today

I have just realised a mistake in my validDate Function. The mistake is that it only validates the day and not the month or year, i also realised that it would validate the day even if the day is zero so I need to fix this. It was only when I was creating the Java GUI that i realised my mistake, the original initial insight looked like this:

SET monthsThirty0 TO [4,6,7,11]
SET monthsThirty1 TO [1,3,5,7,8,10,12]
SET daysInMonth TO 0

IF inArray(month, monthsThirty0)
SET daysInMonth TO 30
ELSE IF inArray(month, monthsThirty1)
SET daysInMonth TO 30
ELSE IF there is no remainder of year/4
SET daysInMonth TO 29
ELSE
SET daysInMonth TO 28

IF day LESS THAN OR EQUAL TO daysInMonth
RETURN True
ELSE
RETURN False

I now realise that the initial insight should have been (new lines are in bold and in red):

SET monthsThirty0 TO [4,6,7,11]
SET monthsThirty1 TO [1,3,5,7,8,10,12]
SET daysInMonth TO 0

IF year > 0
IF month > 0 and month <= 12
IF day > 0
IF inArray(month, monthsThirty0)
SET daysInMonth TO 30
ELSE IF inArray(month, monthsThirty1)
SET daysInMonth TO 30
ELSE IF there is no remainder of year/4
SET daysInMonth TO 29
ELSE
SET daysInMonth TO 28

IF daysInMonth NOT EQUAL TO 0
IF day LESS THAN OR EQUAL TO daysInMonth
RETURN True
ELSE
RETURN False
ELSE
RETURN FALSE

This means the code for the function needs to be changes to (note the extra checks on lines 6, 7 and 17):

def validDate(d, m, y):
    months_thirty0 = [4,6,7,11]
    months_thirty1 = [1,3,5,7,8,10,12]
    daysInMonth = 0

    if y > 0:
        if m > 0 and m <=12:
            if d > 0:
                if inArray(m, months_thirty0):
                    daysInMonth = 30
                elif inArray(m, months_thirty1):
                    daysInMonth  = 31
                elif (y % 4) == 0:
                    daysInMonth = 29
                else:
                    daysInMonth = 28

    if daysInMonth > 0:
        return (d <= daysInMonth)
    else:
        return False