Title: Signature.html
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()
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
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:
print
date_time = raw_input("Enter date and time: ")
if date_time == "end":
break
file_prefix = set_time_stamp(date_time)
print "prefix: ",file_prefix
if file_prefix == False:
continue
print "OK: ", file_prefix
#write_pair(file_prefix, date_time)
print; print "bye..."
Results:
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/>
|
_______________________________________________
Tutor maillist - Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor