[Tutor] No Blank Separator between Date and Time Valid?

Martin Walsh mwalsh at mwalsh.org
Fri Sep 12 02:11:08 CEST 2008


Wayne Watson wrote:
> This program segment allows an invalid date to go undetected. See below.
> 
>     def set_time_stamp(d1):
>         # yyyy/mm/dd hh:mm:ss in, vyyyymmdd_hhmmss.27 out
>         formatin = '%Y/%m/%d %H:%M:%S'
>         d1 = d1.lstrip()
>         try:
>             date1 = datetime(*(time.strptime(d1, formatin)[0:6]))
>         except ValueError:
>             print; print "Invalid date input. Use yyyy/mm/dd hh:mm:ss."
>             return False
<snip>
> 
>     Enter date and time: 2008/1/100:00:30     <- Why is this valid. The
>     fields are not spearated.
>     dout:  20080110_000030
>     prefix:  v20080110_000030.27
>     OK:  v20080110_000030.27
> 


I have confirmed there is a difference in the behavior of time.strptime
between python 2.4 and 2.5, and I assume you're using 2.4.

It is possibly related to this bug (but hard to say for sure without
looking at the source)...

 http://bugs.python.org/issue1340337

... and the subsequent fix for 2.5. But, clearly something changed
between releases.

from http://www.python.org/download/releases/2.5/NEWS.txt
"""
- Bug #1340337: change time.strptime() to always return ValueError when
there is an error in the format string.
- Bug #1290505: Fix clearing the regex cache for time.strptime().
"""

HTH,
Marty


More information about the Tutor mailing list