On Sun, Mar 1, 2009 at 10:04 AM, Wayne Watson <sierra_mtnv...@sbcglobal.net>wrote:
> Ok, how do I do what's mentioned in Subject? > One thing to be aware of - Python datetimes are just that: date + time. If you specify only the time, the date will be filled in with a default value - generally the beginning of the epoch for your particular platform. "datetime" objects (but not "time" objects!) have a method called "strptime" which does exactly what you want - you give it an input string and a format string to tell it how to interpret the input, and it returns a datetime object. Then you can remove the date portion and you're done: >>> import datetime >>> blah = datetime.datetime.strptime("13:01:15","%H:%M:%S") >>> blah datetime.datetime(1900, 1, 1, 13, 1, 15) >>> blah = blah.time() >>> blah datetime.time(13, 1, 15) The format string for strptime uses the same conventions as the strftime() function; they're generally not listed separately. Or you can hack the string into its component parts and create the time from a timetuple: (I'm including the mistakes I made along the way) >>> inDateStr = "13:01:15" >>> inHour = inDateStr[:2] >>> inHour '13' >>> inMin = inDateStr[4:6] >>> inMin '1:' >>> inMin = inDateStr[3:5] >>> inMin '01' >>> inSec = inDateStr[-2:] >>> inSec '15' >>> blah = datetime.time(inHour, inMin, inSec) Traceback (most recent call last): File "<input>", line 1, in <module> TypeError: an integer is required >>> blah = datetime.time(int(inHour), int(inMin), int(inSec)) >>> blah datetime.time(13, 1, 15) >>> Hope that helps. -- www.fsrtechnologies.com
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor