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

Johan Geldenhuys johan at accesstel.com.au
Fri Sep 12 01:33:54 CEST 2008

It all depends how you specify the format of the time you want, look at the


>>> time.strftime('%Y%M%D%H%M%S')




>>> time.strftime('%Y%M%D %H%M%S')

'200820 092055'



Notice in the second statement, I added the space separator between the %D
and %H.


This statement doesn’t fail and the exception doesn’t happen: date1 =
datetime(*(time.strptime(d1, formatin)[0:6]))



From: tutor-bounces+johan=accesstel.com.au at python.org
[mailto:tutor-bounces+johan=accesstel.com.au at python.org] On Behalf Of Wayne
Sent: Friday, 12 September 2008 2:56 AM
To: tutor at python.org
Subject: [Tutor] No Blank Separator between Date and Time Valid?


This program segment allows an invalid date to go undetected. See below.

from datetime import datetime
import time

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()
        date1 = datetime(*(time.strptime(d1, formatin)[0:6]))
    except ValueError:
        print; print "Invalid date input. Use yyyy/mm/dd hh:mm:ss."
        return False
    formatout = '%Y%m%d_%H%M%S'
    dout = date1.strftime(formatout)
    print "dout: ",dout
    return 'v'+date1.strftime(formatout)+".27"

keyopt = 0
while keyopt == 0:
    date_time = raw_input("Enter date and time: ")
    if date_time == "end":
    file_prefix = set_time_stamp(date_time)
    print "prefix: ",file_prefix
    if file_prefix == False:
    print "OK: ", file_prefix
    #write_pair(file_prefix, date_time)

print; print "bye..."


Enter date and time: 2008/1/1 00:00:30     <- Valid input OK
dout:  20080101_000030
prefix:  v20080101_000030.27
OK:  v20080101_000030.27

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

Enter date and time: 2008/1/1 x00:00:30  <- Invalid input caught

Invalid date input. Use yyyy/mm/dd hh:mm:ss.
prefix:  False

Enter date and time: end

bye ...


           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
             (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
              Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet
          "If voting made any difference they wouldn't let us do it." 
                        -- Mark Twain
                    Web Page: <www.speckledwithstars.net/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20080912/42a66a1f/attachment-0001.htm>

More information about the Tutor mailing list