Title: Signature.html
Very good, with a nice insight about default.  While datetime works well for lots of date/time needs, I find it a bit unwieldy. It takes a bit of patience and does not attach to one's memory well, IMHO.

So that solves my problem, but I'll continue by asking if this procedure can be made cleaner?

        set_loc_dict[ "start_time" ]  = self.start_time.strptime("%H:%M:%S")

yields an error, since self.star_time is str and has "no attribute strptime". That's fine, but neither will strftime fit the circumstances here. That leaves this bit of a struggle to get it right:

        x = str(self.start_time)
        set_loc_dict["start_time"] = datetime.datetime.strptime(x,"%H:%M:%S")

It seems like there should be a simpler way to wrestle this to datetime.

Marc Tompkins wrote:
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

--


           Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

             (121.01 Deg. W, 39.26 Deg. N) GMT-8 hr std. time)
            

                "Nature, to be commanded, must be obeyed."
                                   -- Sir Francis Bacon 

                    Web Page: <www.speckledwithstars.net/>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to