Date validation

James Gregory james at anchor.net.au
Fri Apr 25 09:22:51 CEST 2003


Hi all,

I'm writing a killer web app in python and I want to validate all the
input it gets. Amongst said input are dates represented as text strings
of the format 'YYYY-MM-DD' - how can I check whether or not such strings
represent a valid date? I had thought that

    time.strptime('2003-4-31', '%Y-%m-%d')

would throw an exception (since gnome-something tells me that april only
has 30 days), but it just gives me back

    (2003, 4, 31, 0, 0, 0, 3, 121, 0)

indeed I'm even able to use mktime on that without an exception.

The docs for time.strptime inform me that it's libc's fault, which it
probably is, but I'm still stuck for a method to validate this data.

I mean I *could* write this myself, but I had to ask the computer how
many days in April - someone with a better grasp of the date must have
written this stuff before :)

Thanks,

James.







More information about the Python-list mailing list