[Baypiggies] custom date parser

Aaron Maxwell amax at redsymbol.net
Wed Sep 3 19:12:23 CEST 2008

Hi all,

Below is a function that parses a date string in the form "YYYY-MM-DD"
and returns a datetime.date object, or None if it's bad input and
cannot be converted.  However, it does a couple of special tricks.
The data in certain cases is known to have a value for the day or
month that is not in the valid range; e.g., it may be 2007-11-31
(November traditionally only has 30 days), or 2002-14-23.  In this
situation, I want to keep the most signficant good field(s) and set
the lessors to 1, then return the date object from that - so the
results of the above would be date(2007, 11, 1) or date(2002, 1, 1)

The function below does this.  It uses a triply-nested try/except
block, and I can't shake the feeling that there is a shorter and
clearer implementation.  Any thoughts?

Of course, one approach would be to manually check that the month and
day field before passing them to datetime.date.  I would rather reuse
the validation code in the date class, though, for obvious reasons.

Thanks in advance,

import datetime
def parse_datefield(raw_pubdate):
    Parse a datefield
    Takes in a date string in the format YYYY-MM-DD.
    Returns a datetime.date object.
    # ... imagine validation/error checking code here ...
    parts = map(int, raw_pubdate.split('-'))
        d = datetime.date(*parts)
    except ValueError:
        # day out of range?
        parts[-1] = 1
            d = datetime.date(*parts)
        except ValueError:
            # month out of range?
            parts[-2] = 1
                d = datetime.date(*parts)
            except ValueError:
                # give up
                d = None
    return d

Aaron Maxwell

More information about the Baypiggies mailing list